Regex parsing - numeric values with whitespace

D

David

I have rows of 8 numerical values in a text file that I have to parse. Each
value must occupy 10 spaces and can be either a decimal or an integer.

// these are valid - each fit in a 10 character block
123.8
123.8
1234.567
12345
12345
1234.567

// these are not valid
12 34.56 // whitespace in the middle of the value
1234 // occupies 11 spaces

// these are valid lines
// there is one value in each 10 character block
12345.678912345.678912345.678912345.678912345.678912345.678912345.678912345.6789
345.6789 345.6789 45.6789 45.678912345.6789 345.678912345.6789
2345.6789

In reality the data can be in many different formats. (single values, rows
of 8 followed by an integer, rows of 10...) Currently I am parsing this with
code. For this example I read a line, break it up into an array of 8 values
of 10 characters each, and check to see if each one is numeric. If they are
all numeric, move on to the next line.

My biggest problem is how to deal with the whitespace. It can come before
the value, after the value, but not in the middle of the value. The value
can be of any length up to 10. It can be placed anywhere in the 10
character block and padded with whitespace. There are a lot of possible
variations. Also, I would like to generalize it to create the regular
expression on the fly for different formats.

Is there a way, using regular expressions, to specify a total width
including varing amounts of whitespace (depending on the size and position
of the value)?

Thanks for any help.

David
 
N

Nicholas Paldino [.NET/C# MVP]

David,

I don't think that you are going to get any real performance gains using
RegEx here. I also don't think that you are going to get a maintinence gain
either, since I can't think of a regex feature which will let you do this.

On top of that, you already have code that does this. It really doesn't
seem to difficult. Just read each line, read 10 characters, trim it, see if
it is a number, repeat.

Why fix it if it isn't broken?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top