Reputation: 379
I'm not the best at re.
Can anyone tell me if this pattern will work to return a single occurrence of a whole or decimal number before the occurrence of the literal,"each"? The number and the string each will be separated by a single space.
for each in parsed:
if measure_string.find(each)>-1:
r = re.compile("([0-9]\.?[0-9]?) "+each)
b = re.match(r,measure_string)
if b:
return b, each
Thanks for taking a look.
Upvotes: 3
Views: 4355
Reputation: 729
There is a more precise regexp of the real numbers:
"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$"
And some check for this regexp:
import re
realnum=re.compile("^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$")
["yes" if realnum.match(test) else "no" for test in ["12", "+12", "-12", "-3.14", ".314e1", "+.01e-12", "+22.134E+2"]]
['yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes']
["yes" if realnum.match(test) else "no" for test in ["..12", "+-12", "-12.", "-3.14p", ".314e1.9", "+. 01e-12", "+22.134E"]]
['no', 'no', 'no', 'no', 'no', 'no', 'no']
I have added [eE]
option according to Joonho's comment.
Upvotes: 3
Reputation: 81
try:
\b(([1-9][0-9]*)?[0-9]\.[0-9]+)\b
to match reals and
\b(([1-9][0-9]*)?[0-9])\b
to match simple.
nice place to test regexes: http://www.regexr.com/
Upvotes: 1
Reputation: 3781
This will do:
import re
def parse(measure_string,each):
r = re.compile(r"\d+(\.\d*)?(?=\s"+each+")")
b = re.match(r, measure_string)
if(b):
return b.group(0)
we are using a positive look ahead with ?= in order to match the float only if it's followed by whitespace and the string each
, but to not include that whitespace and each
in the match.
We then return the matched string with MatchObject.group(0)
as per the ref.
Upvotes: 0
Reputation: 3185
[0-9]\.?[0-9]?
The first [0-9] will match once occurrence of a digit. The .? will match 0-1 periods [0-9]? will match 0-1 digits.
So, your regex will parse 1, 11, 1.1, but not 1.11 or 11.1
If you want to parse all of the above, I suggest the following.
([0-9]+(?:\.[0-9]+)?)(?:\s)
[0-9]+ - Match 1 or more digits
\. - Match 1 period
[0-9]*? - Match all remaining digits.
()? - Enter this regex 0 or 1 times.
Anything within parenthesis will be captured. If you see a regex enclosed in (?:...) it is a NON-capturing regex. BUT if that (?...) is enclosed in a (...), it will be captured by the (...) regex... it's kind of messy. But the above should work to capture only the number and not the space.
Upvotes: 3