Regular Expression Question

Status
Not open for further replies.
Hey Ars,

I have a set of floating point numbers that I would like to extract from the following line:

AAA 27.9 BBB -47.89 CCC 3.2 DDD 478.345

I've pieced together the following regular expression (Corrected version ((\-*[\d\.]){3}) )but it will only match 27.9 and then stop. Using only a regular expression how would you match just the first 3 numbers (27.9, -47.89 and 3.2)?

Any advice would be greatly appreciated.
-M
 
Thanks for the reply fitten!
fitten":3uhtql84 said:
Your regex doesn't say to do what you're wanting it to do.
Whoops, it should be: ((\-*[\d\.]){3})
Other than that, you might want to consider doing a match and iterating to find each one. Find one number, then keep trying to match until there are no more matches (Or, find the first one and then keep trying until you found all of them or as many as you want.)
What I'm confused about is the syntax to perform an iteration with just regex. The expression (\-*[\d\.]) should match any positive or negative float. I thought that adding {3} afterwards would direct it to match the first 3 instances.

Just to be clear, I'm not using regex with another language. Instead I'm working with an application that supports regex to parse data.
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
What you have matches this:

regex.png


Do a search on Google (or where ever) to find a regex to match a floating point number (positive and negative) and start with that.
 

Alamout

Ars Legatus Legionis
27,285
I thought about mentioning that the RE for the number is not specific enough--it would match ".2.", for instance--but I figure that the input can be assumed to be well-formed, or if not he'll learn about that soon enough.

So, \-?[\.\d]+ is fine for the numbers--it will match a superset of what he wants, but if the wrong stuff isn't actually present then it's no problem.
 

Drizzt321

Ars Legatus Legionis
34,929
Subscriptor++
Wouldn't you want it to be :

([-+]?\d+\.\d{1,3})

Optional dash or plus, one or more digits, period, and one to three digits after the period. Then match that multiple times (globally, or multiple calls, whichever). To get fancy I suppose you could say that the period and digits after it were optional like

([-+]?\d+(\.\d{1,3})?)

which would then match all numbers, positive or negative, floating point or not. Add some \b on either side (word boundry) and you can find them as long as they aren't in the middle of a word.

*YMMV, these have not been tested and are the product of the end of a day.
 
fitten, Alamout, Drizzt321 thank you for the assistance.

@fitten: I purchased a copy of RegexBuddy and it has really helped sort out the logic behind the expressions. Unfortunately the application I'm using does not seem to conform to all Regex syntax as the following expressions yield different results:

1) Original expression:
Code:
\-*[\d\.]+

Works in RegexBuddy, and the application. But it will return only the first number of this line (27.9) and then quit:

Code:
AAA 27.9 BBB -47.89 CCC 3.2 DDD 478.345

After a bit of reading I think I better understand the usage of ?, * and + within parts of an expression. But how would I repeat the entire expression using just regex? Say if the line above contains 25 separate floating point numbers and I would like to capture the first 15 instances.

Does \-*[\d\.]+ automatically parse through the data an unlimited number of times but it is the application which makes it seem to run only once?

2) RegexBuddy's suggestion:
Code:
([-]?[0-9]+(\.+[0-9]+))

Works in RegexBuddy but fails in the application I'm using. It returns a 'Null' character.

3) Alamout's suggestion:
Code:
\-?[\.\d]+

Really elegant solution. Works in RegexBuddy but fails in the application I'm using. It returns a '?' character.

@Alamout: The application being used is a serial data logger. Regex is used to parse the data before exporting it to a document (Excel file). What I'm trying to do is take an arbitrary number of float numbers and put each value into its own cell.

@Drizzt321: Not exactly what I was looking for but that will certainly be helpful to me in the future. That limits the number of decimal places returned not the total number of values. The help is appreciated all the same :)

Thanks again for your help!
-M
 
It's hard to say without having the program in front of us. For one thing, this is the point where regular expressions break down across languages. What implementation is the program using? I need a non-capturing group expression. In Perl 5, it's (?:), so that's what I'm using in mine. I don't think it's the same in every language. Also, between implementations, the expressions are similar, but the actions you can take given the expressions that you have will vary between implementations. Does it only export the capturing groups?

This untested regex ^\D*?(?:(-?\d[\d\.]+).*?)+$ should match the whole line, but only capture the floating point numbers. It uses repeated capturing groups (http://www.regular-expressions.info/captureall.html).

As a side note, - isn't metasyntactic.
 

Alamout

Ars Legatus Legionis
27,285
masonk":2xh5l84z said:
As a side note, - isn't metasyntactic.
I always forget that--it matters inside of brackets but not outside of them. AFAIK, an extraneous \ doesn't hurt, at least in the versions I use.

mental":2xh5l84z said:
Does \-*[\d\.]+ automatically parse through the data an unlimited number of times but it is the application which makes it seem to run only once?
As masonk says, this is where it gets fuzzy. Different implementations will do things differently, but in general a regular expression does not repeat automatically--if you need it to match multiple times, you have to write it so that it does so.

The way the application uses regexes is not quite standard. It seems like you give it a single RE and it tries to match it over and over to the stream of incoming data. Or maybe not, I can't tell.

I would recommend doing some experimenting with very simple input and very simple regexes, if you can. Use the regex 'aaa' and feed it 'aaabbbaaa'. Do you get two matches, or just one? If you get two, then the program is applying the regex globally. If not, then you need something more like what masonk suggested, which has the repeated nature built in to the regex.

3) Alamout's suggestion:
Code:
\-?[\.\d]+

Really elegant solution. Works in RegexBuddy but fails in the application I'm using. It returns a '?' character.
It returns a '?' ? Is that actually present in the input? This might be an error code. Or possibly, as masonk pointed out, the problem is that I needlessly escaped the '-' in the beginning. Perhaps this would work:
Code:
-?[\.\d]+
But the other examples are much more specific to floating-point numbers. This one has the major caveat that it will match other junk in the input.
 
Sorry for the late reply masonk, Alamout.

Got in touch with the company and it was as you surmised a limit of their regex implementation (no capture groups, iteration does not work as expected) that I was encountering. In the end I had to use a separate reg exp for each value, which wasn't too bad. It was a good learning experience as I've never had used regex before.

Thanks again to everyone for your help!
-M
 

A.W.

Ars Tribunus Angusticlavius
6,790
Got in touch with the company and it was as you surmised a limit of their regex implementation (no capture groups, iteration does not work as expected) that I was encountering.
I would suggest that you check out The Regulator. It's old and hasn't been updated in a while, and its intellisense is crashy (I always just turn that off right away), but it does support capture groups and the like. It is limited to .NET's regex language, but for something like this, it would probably be fine.
 
Status
Not open for further replies.