I have strings similar to this example:
str = ' area AMW1 = 93.3 m2 ';
And I would like to only extract the floating point number (possibly with sign "-") 93.3
. The float I like to extract is always surrouned by white spaces.
How can I do that?
I tried
s = regexp(str,'\d \.?\d*','match')
However, it matches also the 1
and the 3
. Various other expressions I found do not work neither...
Thank you.
CodePudding user response:
You can use
regexp(str,'-?\d \.\d ','match')
Or, if you need to also match a
:
regexp(str,'[- ]?\d \.\d ','match')
If you need to match within whitespace boundaries only:
regexp(str,'(?<!\S)[- ]?\d \.\d (?!\S)','match')
If the float value must be in between two whitespace chars:
regexp(str,'(?<=\s)[- ]?\d \.\d (?=\s)','match')
Details
(?<=\s)
- right before the match, there must be a whitespace(?<!\S)
- right before the match, there must be start of string, or a whitespace[- ]?
- an optional-
\d
- one or more digits\.
- a dot\d
- one or more digits(?!\S)
- right after, there must be the end of string or a whitespace(?=\s)
- right after the match, there must be a whitespace.
And if you need to find both integer or floats, replace \d \.\d
with \d (?:\.\d )?
.
CodePudding user response:
try this:
re.compile(r'(?<=\s)\d (\.\d )?(?=\s)')