python regex numbers only

Solutions on MaxInterview for python regex numbers only by the best coders in the world

showing results for - "python regex numbers only"
Jason
16 Apr 2019
1import re
2re.compile(r'[/d]+') 
3# /d refers to any digit
4# use other regex patters for ore modifcations
5# This can extract the numbers from the regex 
Gabriele
21 Jan 2017
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"""
queries leading to this page
python regex 4 digitregular expression find number in stringextract number from string python regexregex for numbers only in pythonregex with numbers pyhon regex python get only numbersand pythonmatch number regex pythonhow to get numbers from string regex pythonpython regex extract only numbersregex python match string and numbers pythonpython regex intpython regex digit 1 to 3only digits regex pythonpython regex only digitsget number from string regex pythonpython extract number from string regexregex with numbers pythonnumber only regex pythonregex only letters and numbers pythonnumber regex pythonregex to extract number from string python with thainfind number with regular expression pythonregex in python for numbersregex to match only numbers pythonregular expression python just numberpython regex get numbers and lettersregex to accept letters and numbers pythonpython regex match 4 numbersextract numbers fromstring using regex pythonpython regex after numberspython only numbers regexregex compile a number as string pythonpython regex match numbersregex to match number pythonpython regex non numberssearch numbers regex pythonregex match string followed by number pythonregex python match only digitspython regex match number in stringregex python integersregular expression numbers only pythonregex for only numbers pythonpython regex check number onlypython regex number onlypython regex to extract numberregex filter only numbers pythonnumeric regex in pythonpython regex to take only numbersregex number only pythonregex grab numeric values in str python regex to check number before certain string in pythonregex keep only numbers pythonregex in python for numeric string matchpython regex pattern digitregular expression for numbers pattern pythonpython regex for digitscheck for the numbers regular expression pythonfind numbers regex pythonregular expression for numbers only pythonpython regexp numberspython regex int from stringregex find integer pythonregex get only numbers pythonpython regex get only numbersextract numbers from string python regexregex for strcit numbers only pythonpython regex letter or numberpython regex just get numberspython regex match letters and numbersregex specified int pythontake numbers from string python regexmatch number 1 regex pythonregex take only numbers pythonpython regex all integersonly numbers regex pythonpython regex numberpython number regexpython code for number regexpython regex match all only numbersget all numbers regex pythonpython regex match only numbersany number of numbers regex pythonhow to find numbers with pattern in string pythoncheck if all values are numbers python regexpython regex n digitspython regex match numberpyton regex for numbers including efinding numbers with regex pythonpython regexp numberextract name and number from strring using regex pythonregular expression python to get only numbersregex to get only numbers from a string pythonpython regex find numberregex to keep only numbers pythonpython extract number from string regular expressionpython regex get digitsregex for numbers in pythonpython regex any numberfind digits in string python regexregex for specific number in pythonpython regex digitregex to detect only numbers pythonint only regex pythonregexp python numberpython regex for numbers onlypython regex find number in stringpython regex find numbers in matchregex for numbers only pythonregular expression in python for numbersregex all numbers pythonpython find numbers in string regexregular expression python numbers onlycreate a regex for number verification regex in pythonregex in python for only intergersregex only numbers pythonpython regex for number in stringregex python find all numbersregex for numbers only from a string pythonpython regular expression specific number of characterspython regex non numericregex python numbersregular expression python extract string from numberpython regex keep only numbershow to print all the number in a string using regular expression in pythonpython regex to find numbersfind number regex pythonget numbers from a text python regexregular expression python extract numbersregex python only numberspython regex only numberspython regex to see if a string are numbers onlyhow to find number in regular expression pythonregex python only integersregular expression numbers pythonpython regex find number in paternregex to match numbers pythonpython regex no number after patternpython regex for containing only numbersregex to allow only numbers and lettes in pythonpython regex numberspython regex for numberspython regex to extract number from stringpython regex get number from stringextract number from string with regex in pythonnumber in regex pythonregex to get only numbers pythonregular expression python separate number and stringregex for only number and letter input pythonregex for digits only pythonpython regex find string number length 3d 6regex numbers from a text pythonpython regex 4 digitsnon number regular expression pythonpython regex only 4 digitsregex extract number from string pythonpython regex find numbers after stringpython regex 23 then any numbershow to extract numeric values from a string in python regexregex only numbers and letters pythonpython regex number string numberregex only get numeric in a string in pythonregex to return numbers in a string pythonregular expression for numbers pythonpython regex numbers onlypython regex extract numbers from stringregular expression to extract number from string pythonpython regex extract only numbers from a stringregex to extract number from string pythononly 1 digit regex pythonhow to extract numbers from a string in python using regexregex only match numeric data in string in pythonregex python get numberregex single number pythonpython regex take only numberspython regex match digits regex get number from string pythonregex for numbers pythonfind digits in regex pythonregex to identify numbers pythonregex find integers in string pythonpython regex for real numberspython regex get only numbers from stringregex numbers only pythoncheck number regex pythonpython regex find numbers regex find all digits in string pythonpython regex numbers only