Python regex to extract positive and negative numbers between two special characters
In general, when you want to extract positive or negative integer or float numbers from text using Python regex, you can use the following pattern
re.findall(r'[-+]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?', text)
See this regex demo. Note: \d{1,3}(?:,\d{3})+
alternative matches integer numbers with comma as a thousand separator. You may adjust it to match the thousand separator you need, say, \xA0
if the thousand separator is a non-breaking space, or \.
if it is a dot, etc.
Some more options will look like
re.findall(r'[-+]?\d+(?:\.\d+)?', text) # Integer part is compulsory, e.g. 5.55
re.findall(r'[-+]?\d*\.?\d+', text) # Also matches .57 or -.76
Here, you want to extract any number in between >
and <
chars.
You may use
re.findall(r'>([-+]?\d[\d,.]*)<', text)
See the regex demo.
Details
-
>
- a>
char -
([-+]?\d[\d,.]*)
- Group 1:-
[-+]?
- an optional-
or+
-
\d
- a digit -
[\d,.]*
- 0 or more digits,,
or.
-
See the Python demo:
import re
st='''["FL gr_20 T3\'><strong>+1,921.15</strong>"]' st='["FL gr_20 T3\'><strong>-921.15</strong>"]' st='["FL gr_20 T3\'><strong>21.15</strong>"]' st='["FL gr_20 T3\'><strong>1,11,921.15</strong>"]' st='["FL gr_20 T3\'><strong>1,921</strong>"]' st='["FL gr_20 T3\'><strong>112921</strong>"]' st='["FL gr_20 T3\'><strong>1.15</strong>"]' st='["FL gr_20 T3\'><strong>1</strong>"]'''
print(re.findall(r'>([-+]?\d[\d,.]*)<', st))
# => ['+1,921.15', '-921.15', '21.15', '1,11,921.15', '1,921', '112921', '1.15', '1']