・ 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.
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'
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
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
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'>
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
print(re.match('\d{2}',text5))
print(re.search('\d{2}',text5))
Execution result
None
<re.Match object; span=(1, 3), match='12'>
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'>
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']
print(re.findall('\d{3}\d{4}',text8))
Execution result
['1231234', '9999999']
print(re.split('[:]',text9))
Execution result
['〒123-1234', 'Tokyo', 'Chuo-ku 999-9999']
print(re.split('[,]',text9))
Execution result
['〒123-1234:Tokyo:Chuo-ku 999-9999']
Recommended Posts