03-05-2009 04:29 PM
Hi everyone,
I got another more complicated one here.
+0,+0.00000000E+000,
I need to put -4.76517000E+001,-4.64744200E+001,-6.18140500E+001,-5.93055600E+001 into an array.
I am using a regular expression as '[+-]([0-9]+[1-9]+)[E][+]\d', just wondering what is happening there.
Any idea is well appreciated,
Thanks,
+Kunsheng
03-06-2009 12:38 AM - edited 03-06-2009 12:47 AM
Kunsheng Chen wrote:
I need to put -4.76517000E+001,-4.64744200E+001,-6.18140500E+001,-5.93055600E+001 into an array.
I am using a regular expression as '[+-]([0-9]+[1-9]+)[E][+]\d', just wondering what is happening there.
The regexp above is not right one for you.
In this regular expression you will match the following things:
First, it should be the sign + or -: [+-]([0-9]+[1-9]+)[E][+]\d. So you will match strings like +01E+0, but not 01E+0 (because sign missing)
Then it should be the one or more number in range 0-9: [+-]([0-9]+[1-9]+)[E][+]\d'. Here you will match strings like +01E+0, but not +E+0 (because no digits follow the sign)
Then it should be one or more number in range 1-9: [+-]([0-9]+[1-9]+)[E][+]\d'. Here you will match strings like +21E+0, but not +20E+0 (because zero present instead of 1...9)
Then 'E' is expected: [+-]([0-9]+[1-9]+)[E][+]\d. Here you will match strings like +35E+0, but not +35+0.
Then '+' sign should present: [+-]([0-9]+[1-9]+)[E][+]\d. Here you will match strings like +01E+0, but not +01E-0 (because '-' instead of '+')
Then it should be digit: [+-]([0-9]+[1-9]+)[E][+]\d. (Note, that \d notation equivalent with [0-9]). So, regexp will match +01E+0, but not +01E+, and from '+01E+001' the only '+01E+0' will be matched (because single digit is required).
So, from the string '+0,+00E+0,+01E+0,+20E+0,-03E+8,03E3,+E+0,+21E+0,+20E+0,+35+0, +01E+0, +01E+001' the regexp above will match '+01E+0', '-03E+8', '+21E+0', '+01E+0' and '+01E+0'.
From given string '-4.76517000E+001,-4.64744200E+001,-6.18140500E+001,-5.93055600E+001' the regexp above will match nothing.
Feel difference with this string: '-476517001E+001,-464744200E+001,-618140502E+001,-593055600E+001'. Here two matches: '-476517001E+0' and '-618140502E+0'
Well, for the digits above the simples way is following '[+-]\d+\.\d+[E][+-]\d+':
First the sign [+-]\d+\.\d+[E][+-]\d+. This will match -4.76517000E+001 or +4.76517000E+001
Then one or more digits: [+-]\d+\.\d+[E][+-]\d+. This will match -4.76517000E+001 or -45.76517000E+001. If you don't need matches like -45.76517000E+001, then just remove + after \d. Then -4.76517000E+001 will be mnatched, but -45.76517000E+001 will be not matched.
Then period: [+-]\d+\.\d+[E][+-]\d+. This will match -6.18140500E+001, but not -6..18140500E+001,
Then one or more digits again: [+-]\d+\.\d+[E][+-]\d+ This match -5.93055600E+001,
Then 'E' as described above [+-]\d+\.\d+[E][+-]\d+
Then sign [+-]\d+\.\d+[E][+-]\d+
and finally one or more digits [+-]\d+\.\d+[E][+-]\d+ for matching -6.18140500E+001. Take a note, that \d without '+' will match single digit only. So, regexp '[+-]\d+\.\d+[E][+-]\d' will give you '-6.18140500E+0'
With the parentheses in regexp you can group that part of the regular expression together and get submatches:
For example [+-]((\d+)\.(\d+))[E][+-]\d+ will give you the following result:
Can't explain more detailed, sorry.
Regards,
Andrey.