473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regex parsing - numeric values with whitespace

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.6789 12345.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
May 10 '06 #1
1 2676
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <dr*******@yahoo.com> wrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
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.6789 12345.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

May 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Ray Stevens | last post by:
I would like to quickly strip out all non-numeric characters from a string (including whitespace) into a new string (i.e., remove the edit mask). Would RegEx be the best way to do this and, if so,...
6
by: Lubomir | last post by:
Hi, I am using the following pattern: "\\b" + MySttring + "\\b" If MyString is "one", this should pick up whole words like "one". The problem is, it will pick up also the word: "one.two"...
3
by: Just Me | last post by:
I need a regex to do this. Ignore < possibleWhiteSpace htmlTag Replace whitespace anything > With > Basically I need to remove anything following the html tag up to and
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method...
4
by: CJ | last post by:
Is this the format to parse a string and return the value between the item? Regex pRE = new Regex("<File_Name>.*>(?<insideText>.*)</File_Name>"); I am trying to parse this string. ...
3
by: Jeff | last post by:
I'm parsing this: name="value" and sometimes it looks like this: name2="value2 without the closing '"'. I don't want to capture the end quote.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.