[PYTHON] Extract numbers with regular expressions

Regular expressions

Numerical extraction

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)

result

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

Extract numbers with regular expressions
Handling regular expressions with PHP / Python
Ansible Jinja2 filters Replace and extract variable strings with regular expressions
[Python] Regular Expressions Regular Expressions
Python: Simplified morphological analysis with regular expressions
[Python] Get rid of dating with regular expressions
Remove extra strings in URLs with regular expressions
Regular expression with pymongo
Extract EXIF with sips
FizzBuzz with regular expressions etc. without using the'%' operator
Extract non-numeric elements with pandas.DataFrame
Extract Twitter data with CSV
Extract arbitrary strings using Python regular expressions / Use named groups
Use regular expressions in C
Use regular expressions in Python
Get rid of dirty data with Python and regular expressions
Regular expression manipulation with Python
About Python and regular expressions
Extract peak values with scipy
Sorting with mixed numbers and letters
Extract the xz file with python
Testing with random numbers in Python
[Language processing 100 knocks 2020] Chapter 3: Regular expressions
Extract the maximum value with pandas.
[Automation] Extract Outlook appointments with Python
3.6 Text Normalization 3.7 Regular Expressions for Tokenizing Text
slackbot memorandum ~ Request using regular expressions ~
I can't remember Python regular expressions
Decompose hostname with co.jp with regular expression
By language: Regular expressions for passwords
[Beginner] Extract character strings with Python
Extract specific multiple columns with pandas
Extract multiple elements with Numpy array
100 Language Processing Knock 2020 Chapter 3: Regular Expressions
When using regular expressions in Python