正規表現
Python
マッチ判定
code: python
import re
content = 'hellow python, 123, end.'
pattern = r'hel'
result = re.match(pattern, content) # 先頭がパターンにマッチする(例えばaaahellowにはマッチしない)
result = re.search(pattern, content) # 文全体がパターンにマッチする(推奨)
print(result)
# output:<_sre.SRE_Match object; span=(0, 3), match='hel'>
print(result.span())
# output:(0, 3)
print(result.group())
# output:hel
code: python
import re
content_str = 'aaa_123_456_bbb'
found_str_list = re.findall(r'\d+', content_str)
print(found_str_list0) # 123 print(found_str_list1) # 456