regular expression to find numbers in a string python

Solutions on MaxInterview for regular expression to find numbers in a string python by the best coders in the world

showing results for - "regular expression to find numbers in a string python"
Ryker
15 Oct 2019
1import re
2
3# Example with integers and floats both positive and negative and scientific notation.
4target_str = 'I live at 9-162 Malibeu. My phone number is +351911199911. I have 5.50 dollars with me, but I have a net income of -1.01 per day which is about -1 dollar a day with an error of +-.01. Also the earth has a mass of 5.972e24 kg or about 6e24 kg.'
5# Depending on what you want (p=positive, n=negative):
6regex_expressions = {
7    'p_ints' :            "\d+",
8    'pn_ints' :           "[-+]?\d+",
9    'p_floats' :          "\d*\.\d+",
10    'pn_floats' :         "[-+]?\d*\.\d+",
11    'scientific_notation':"[-+]?\d+(?:\.\d+)?e[-+]?\d+",
12    'pn_floats_or_ints' : "(?:[-+]?)(?:\d*\.\d+|\d+)",
13    'universal':          "(?:[-+]?)(?:\d+(?:\.\d+)?e[-+]?\d+|\d*\.\d+|\d+)"
14}
15
16regex_results = dict()
17
18for target_type, regex_expression in zip (regex_expressions.keys(), regex_expressions.values()):
19    regex_results[target_type] = re.findall(regex_expression, target_str)
20    print(target_type,':',regex_results[target_type])
21
22print ('\nThese results are still strings, but can easily be turned into floats or ints:')
23for number in regex_results['universal']:
24    print(float(number))
25
26"""
27Used RegEx symbols:
28    [] : look for any character inside the brackets
29    \d : look for any digit
30    \. : look for a dot (.)
31    + : look for one or more occurences of the previous expression
32    * : look for zero or more occurences of the previous expression
33    ? : look for zero or one occurences of the previous expression
34    (?:...) : create a non-capturing group
35    | : look for either of the previous expressions (OR operator)
36    
37
38Short explanation of each regex:
39    -> positive integers: \d+
40        look for one or more digits
41    -> positive or negative integers: [-+]?\d+
42        look for one or more digits, potentially preceded by a '-' or a '+'
43    -> positive floats: \d*\.\d+
44        look for zero or more digits, followed by a dot, followed by one or more digits (a lazy representation such as '.3' works in this case). Scientific notation is not allowed.
45    -> positive or negative floats: [-+]?\d*\.\d+]
46        look for zero or more digits, followed by a dot, followed by one or more digits, potentially preceded by a '-' or a '+'
47    -> scientific notation: [-+]?\d+(?:\.\d+)?e[-+]?\d+
48        look for any '+' or '-' signs, if they exist. Look for one or more digits, potentially followed by a dot and decimal part. Look for an 'e', followed by one or more digits
49    -> any number not in scientific notation: (?:[-+]?)(?:\d*\.\d+|\d+)
50        look for any '+' or '-' signs, if they exist. Look for zero or more digits, followed by a dot, followed by one or more digits (float) OR look for one or more digits (integer).
51    -> any number: (?:[-+]?)(?:\d*\.\d+|\d+|\d?e[-+]?\d?)
52        basically look for '+' or '-' and then do an OR between the previous expressions using non capturing groups.
53"""
54
55"""
56OUTPUT:
57    p_ints : ['9', '162', '351911199911', '5', '50', '1', '01', '1', '01', '5', '972', '24', '6', '24']
58    pn_ints : ['9', '-162', '+351911199911', '5', '50', '-1', '01', '-1', '01', '5', '972', '24', '6', '24']
59    p_floats : ['5.50', '1.01', '.01', '5.972']
60    pn_floats : ['5.50', '-1.01', '-.01', '5.972']
61    scientific_notation : ['5.972e24', '6e24']
62    pn_floats_or_ints : ['9', '-162', '+351911199911', '5.50', '-1.01', '-1', '-.01', '5.972', '24', '6', '24']
63    universal : ['9', '-162', '+351911199911', '5.50', '-1.01', '-1', '-.01', '5.972e24', '6e24']
64    
65    These results are still strings, but can easily be turned into floats or ints:
66    9.0
67    -162.0
68    351911199911.0
69    5.5
70    -1.01
71    -1.0
72    -0.01
73    5.972e+24
74    6e+24
75"""