import re
p2b = open('repattern2b.txt').read().rstrip()
I need to write a regular expression pattern that matches strings representing numbers written in scientific notation. But in addition for this pattern , ensure group 1 is the sign of the mantissa (if there is a sign); group 2 is the mantissa, but only if it is not 0 (that exception makes the pattern simpler); group 3 is the exponent.
for example: If
m = re.match(the pattern,’9.11x10^-31’)
then m.groups() is
(None, '9.11', '-31').
There should be no more groups.
Below is the regular expression I wrote for 'repattern2b.txt':
^([+-]?)([1-9].[0-9]+)x10^([1-9][0-9]*)$
But I got the error:
54 *Error: re.match(p2b,'0').groups() raised exception; unevaluated: (None, None, None)
55 *Error: re.match(p2b,'5').groups() raised exception; unevaluated: (None, '5', None)
56 *Error: re.match(p2b,'5.0').groups() raised exception; unevaluated: (None, '5.0', None)
57 *Error: re.match(p2b,'5.2x10^31').groups() raised exception; unevaluated: (None, '5.2', '31')
58 *Error: re.match(p2b,'5.2x10^-31').groups() raised exception; unevaluated: (None, '5.2', '-31')
59 *Error: re.match(p2b,'5.2x10^+31').groups() raised exception; unevaluated: (None, '5.2', '+31')
60 *Error: re.match(p2b,'-5.2x10^-31').groups() raised exception; unevaluated: ('-', '5.2', '-31')
It seems that my regular expression raises an exception, but I am not sure why. Can someone help me to fix it? Thanks in advance.