About Python and regular expressions

Make a note of what you might use often about regular expressions.

・ Mac ・ Python


Memo content
① "." Is any one character (including numbers) other than line breaks (2) match is a function that determines whether to match from the beginning. If it matches, it returns a match object (a collection of regular expressions), and if it does not match, it returns NONE. ③ search is a function that determines whether or not there is a match in the middle. If there are two locations, return only the first one. If it matches, it returns a match object, otherwise it returns NONE. ④ split is a function that splits by the specified character, and the return value is a list type. ⑤ findall is a function that returns ** all ** of the specified characters, and the return value is a list type.

(1) Example

import re

text1 = 'abcde'
text2 = 'a'
text3 = '1234'
text4 = 'a1234'
text5 = 'a1234a567'
text6 = 'The address is 123-3456 Chuo-ku, Tokyo 999-9999'
text7 = '〒123-1234 Chuo-ku, Tokyo 999-9999'
text8 = '9999999, Chuo-ku, Tokyo 1231234'
text9 = '〒123-1234:Tokyo:Chuo-ku 999-9999'
text10 = '〒123-1234 Chuo-ku, Tokyo:999-9999'

(2) Execution content

① When text1 ='abcde'

print(re.match('.',text1))
print(re.match('abc',text1))
print(re.match('abc$',text1))
print(re.match('\d\d',text1))

Execution result

<re.Match object; span=(0, 1), match='a'>
<re.Match object; span=(0, 3), match='abc'>
None
None

② When text2 ='a'

print(re.match('.',text2))
print(re.match('abc',text2))
print(re.match('abc$',text2))
print(re.match('\d\d',text2))

Execution result

<re.Match object; span=(0, 1), match='a'>
None
None
None

③ When text3 = '1234'

print(re.match('.',text3))
print(re.match('abc',text3))
print(re.match('abc$',text3))
print(re.match('\d\d',text3))

Execution result

<re.Match object; span=(0, 1), match='1'>
None
None
<re.Match object; span=(0, 2), match='12'>

④ When text4 ='a1234'

print(re.match('.',text4))
print(re.match('abc',text4))
print(re.match('abc$',text4))
print(re.match('\d\d',text4))

Execution result

<re.Match object; span=(0, 1), match='a'>
None
None
None

⑤ When text5 ='a1234a567'

print(re.match('\d{2}',text5))
print(re.search('\d{2}',text5))

Execution result

None
<re.Match object; span=(1, 3), match='12'>

⑥ text6 ='Address is 123-3456 999-9999, Chuo-ku, Tokyo'

print(re.match('\d{2}',text6))
print(re.search('\d{2}',text6))
print(re.match('\d{3}-\d{4}',text6))
print(re.search('\d{3}-\d{4}',text6))
print(re.search('.\d{3}-\d{4}',text6))

Execution result

None
<re.Match object; span=(3, 5), match='12'>
None
<re.Match object; span=(3, 11), match='123-3456'>
<re.Match object; span=(2, 11), match='123-3456'>

⑦ In the case of text7 ='999-9999, Chuo-ku, Tokyo 123-1234

print(re.match('\d{3}-\d{4}',text7))
print(re.search('\d{3}-\d{4}',text7))
print(re.search('.\d{3}-\d{4}',text7))
print(re.findall('\d{3}-\d{4}',text7))
print(re.findall('\d{3}\d{4}',text7))
print(re.findall('.\d{3}-\d{4}',text7))

Execution result

None
<re.Match object; span=(1, 9), match='123-1234'>
<re.Match object; span=(0, 9), match='〒123-1234'>
['123-1234', '999-9999']
[]
['〒123-1234', 'Ward 999-9999']

⑧ text8 ='In the case of' 9999999, Chuo-ku, Tokyo 1231234'

print(re.findall('\d{3}\d{4}',text8))

Execution result

['1231234', '9999999']

⑨ text9 ='〒123-1234: Tokyo: Chuo-ku 999-9999'

print(re.split('[:]',text9))

Execution result

['〒123-1234', 'Tokyo', 'Chuo-ku 999-9999']

⑩ text10 ='Chuo-ku, Tokyo 123-1234: 999-9999'

print(re.split('[,]',text9))

Execution result

['〒123-1234:Tokyo:Chuo-ku 999-9999']

Recommended Posts

About Python and regular expressions
[Python] Regular Expressions Regular Expressions
Overlapping regular expressions in Python and Java
About python objects and classes
About Python variables and objects
Use regular expressions in Python
About Python, len () and randint ()
About Python datetime and timezone
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
About _ and __
Python tuple comprehensions and generator expressions
About installing Pwntools and Python2 series
I can't remember Python regular expressions
About python dict and sorted functions
About dtypes in Python and Cython
Introductory Python Modules and conditional expressions
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
Handling regular expressions with PHP / Python
When using regular expressions in Python
Get rid of dirty data with Python and regular expressions
About python comprehension
About Python tqdm.
About python yield
Regular expressions that are easy and solid to learn in Python
Python comprehension (list and generator expressions) [additional]
About python inheritance
About PyQt signal, connect and lambda expressions
About python, range ()
(Python) HTML reading and regular expression notes
About python decorators
About python reference
How to use regular expressions in Python
About Python decorators
[Python] About multi-process
Python: Simplified morphological analysis with regular expressions
Talking about old and new Python classes
Talking about Python class attributes and metaclasses
[Python] A game that uses regular expressions when, where, who, and what
Pharmaceutical company researchers summarized regular expressions in Python
Python pandas: Search for DataFrame using regular expressions
Think about depth-priority and width-priority searches in Python
About the difference between "==" and "is" in python
[Python] Get rid of dating with regular expressions
A story about modifying Python and adding functions
[Python] Learn about asynchronous programming and event loops
About the * (asterisk) argument of python (and itertools.starmap)
About shallow and deep copies of Python / Ruby
[python] Compress and decompress
About Python for loops
About Class and Instance
Summary about Python scraping
About function arguments (python)
Python and numpy tips
[Python] pip and wheel
Python iterators and generators
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
Python packages and modules