Extract numbers with regular expressions
import re
#Regular expressions
pattern=r'([+-]?[0-9]+\.?[0-9]*)'
#Search text
text = 'Test+12 5.14 5, goo -8.36 36. 36.ATT'
print('pattern:',pattern,'text:',text)
print('match:',re.match(pattern,text))
print('search:',re.search(pattern,text))
print('findall:',re.findall(pattern,text))
#Save to list
lists=re.findall(pattern,text)
pattern: ([+-]?[0-9]+\.?[0-9]*) text: Test+12 5.14 5, goo -8.36 36. 36.ATT
match: None
search: <_sre.SRE_Match object; span=(4, 7), match='+12'>
findall: ['+12', '5.14', '5', '-8.36', '36.', '36.']
Recommended Posts