Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I want create/write a regular expression for exact match of 6.4 and 2.5 in following string: “abc 6.4 xyz 2.5”

Please help or share your thoughts.

Regards,
YRishi
Posted

.*\s(\d\.\d)\s.*\s(\d\.\d) Or, if the numbers can be longer (e.g. 12.34) .*\s(\d+\.\d+)\s.*\s(\d+\.\d+)
 
Share this answer
 
Comments
Kenneth Haugland 9-Aug-12 10:26am    
(.*\s(\d\.\d)\s.*\s(\d\.\d)). Dont understand what you are doing with the .* sign in the middle. Why not stop it half way?
lewax00 9-Aug-12 12:52pm    
The parenthesis around it aren't needed, he only wants the numbers not the whole string. Let me break it down: any text (abc), whitespace( ), (number (6), decimal point(.), number(4)), whitespace( ), any text(xyz), whitespace( ), (number(2), decimal point(.), number(5)). I could leave out the "\s"s, but that would also match something like abc4.5xyz2.5, which might not be desired. Specifically I'm trying to match the whole line, because he hasn't provided any other examples that's usually the safest bet.
Kenneth Haugland 9-Aug-12 13:10pm    
Aha, so you want to capture the whole string, now I see :) I wondered if I should show OP named capture groups (very useful) but decided not to. A little too complicated ;)
You must see this page:
http://regexlib.com/[^]

You can test your regular expression here:
http://regex.larsolavtorvik.com/[^]

Hope you can find it useful =)
 
Share this answer
 
v2
This is simple, the expreccion could look like this:
(\s\d\[.]\d\s)

You should check out this tool fo rwriting Regular Expressions. It sure helpt me a lot in the past:
Expresso - A Tool for Building and Testing Regular Expressions[^]
 
Share this answer
 
Comments
lewax00 9-Aug-12 12:53pm    
the \s should not be in the parentheses, because he probably only wants to capture the number, not the whitespace.
Kenneth Haugland 9-Aug-12 13:08pm    
Not bad :) And you are right, althoug hes is proberbly going to call Cdbl or CDec on the string, and that should resolve it ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900