[PYTHON] Regular expression re

import re

"""
match()Determine if it matches the regular expression at the beginning of the string
search()Manipulate strings to find out where regular expressions match
findall()Find all substrings that match the regular expression and return them as a list
finditer()Returns a unique match object iterator
"""

# .Is any one character
#A match object is returned
m = re.match('a.c', 'abc')
# group()You can see the contents at.
print(m.group())

#A match object is returned
m = re.search('a.c', 'test abc test abc')
print(m)
print(m.span())
print(m.group())

m = re.findall('a.c', 'test abc test abc')
print(m)

#Match object iterator is returned
m = re.finditer('a.c', 'test abc test abc')
print([w.group() for w in m])

#Repeat 0 or 1
m = re.match('ab?', 'a')
print(m)
#Repeat 0 or more times
m = re.match('ab*', 'abb')
print(m)

#Repeat one or more times
m = re.match('ab+', 'abbb')
print(m)

#Repeat 3 times
m = re.match('a{3}', 'aaaa')
print(m)

# 2-Repeat 4 times
m = re.match('a{2,4}', 'aaaa')
print(m)

#Alphanumeric and underscore,[]The part of is called a set.
m = re.match('[a-zA-Z0-9_]', '1')
# m = re.match('\w', '1')
print(m)

#Other than alphanumeric characters and underscore
m = re.match('[^a-zA-Z0-9_]', '@')
# m = re.match('\W', '1')
print(m)

#Any number
m = re.match('[0-9]', '1')
# m = re.match('\d', '1')
print(m)

#Other than arbitrary numbers
m = re.match('[^0-9]', '@')
# m = re.match('\D', '1')
print(m)

#a or b
m = re.match('a|b', 'b')
print(m)

#Treat as a lump
m = re.match('(abc)+', 'abcabc')
print(m)

#space
m = re.match('\s', ' ')
print(m)

#Other than space
m = re.match('\S', '1')
print(m)

#escape
m = re.match('\*', '*')
print(m)
m = re.match('\?', '?')
print(m)

#First match
m = re.search('^abc', 'abctest abc')
print(m)
#end
m = re.search('abc$', 'test abc')
print(m)

Execution result:

abc
<re.Match object; span=(5, 8), match='abc'>
(5, 8)
abc
['abc', 'abc']
['abc', 'abc']
<re.Match object; span=(0, 1), match='a'>
<re.Match object; span=(0, 3), match='abb'>
<re.Match object; span=(0, 4), match='abbb'>
<re.Match object; span=(0, 3), match='aaa'>
<re.Match object; span=(0, 4), match='aaaa'>
<re.Match object; span=(0, 1), match='1'>
<re.Match object; span=(0, 1), match='@'>
<re.Match object; span=(0, 1), match='1'>
<re.Match object; span=(0, 1), match='@'>
<re.Match object; span=(0, 1), match='b'>
<re.Match object; span=(0, 6), match='abcabc'>
<re.Match object; span=(0, 1), match=' '>
<re.Match object; span=(0, 1), match='1'>
<re.Match object; span=(0, 1), match='*'>
<re.Match object; span=(0, 1), match='?'>
<re.Match object; span=(0, 3), match='abc'>
<re.Match object; span=(5, 8), match='abc'>

Documentation

Recommended Posts

Regular expression re
Regular expression Greedy
Regular expression with pymongo
Date notation regular expression
Regular expression look-ahead, after-yomi
python regular expression memo
Regular expression in Python
Regular expression in Python
Regular expression confirmation quiz!
Julia Quick Note [04] Regular Expression
Regular expression manipulation with Python
Regular expression check tool summary
Decompose hostname with co.jp with regular expression
String replacement with Python regular expression
100 language processing knocks 2020: Chapter 3 (regular expression)
Introduction to regular expression processing system
The reason why the Regular Expression (RE) fail to parse .tex source.
Start / end match in python regular expression
(Python) HTML reading and regular expression notes
Lambda expression
Search pythondict dictionary key by regular expression
How to write regular expression patterns in Linux
Regular expression symbolic group name in Python / Ruby