tags:

views:

93

answers:

2

I need to parse a String having entry 5.55FIX, 5.55 Variable, 5.55Float. I need to extract the decimal value and the string as separate. please suggest?

+2  A: 

Depending on where you're going to use the regex, one of this will surely do the job. Group 1 will contain the number, group 2 will contain the string. Of course this is assuming the string never begins with a number and without a whitespace between the actual number (in which case it couldn't even be told apart by a human unless he knows either the value of the number or that of the string in advance).


(\d+\.\d+)\W*(\w+)
(\d+\.\d+)\p{Z}*(\w+)

As suggested by Drew Noakes, if you only care about the decimal part you may also use one of these:


\d+(\.\d+)\W*(\w+)
\d+(\.\d+)\p{Z}*(\w+)

As suggested by David Andres, you can improve the expressions by adding ^ at the beginning and $ at the end if you're going to use the regex on strings made up only in the way you've stated in your post. This means that with those two characters, "5.55FIX" will match but "Value: 5.55FIX" will not.

emaster70
Unless you expect to match numbers of the format `55.` then the regex could be enhanced so that the number formatting portion changes from `\d+\.\d+` to `\d+(\.\d+)?`
Drew Noakes
@emaster: Add some flanking ^$ characters and you're good.
David Andres
A: 

Another solution to fit your exact questions would be...

^(5\.55)(FIX)$
JustSmith
You answer only the title... Although the body is a bit confusing as well.
PhiLho