Python doesn't seem to have regular expression literals like perl or ruby
Import the re module when using regular expressions
import re
Prefixing a string literal with r disables the escape sequence for that string. (Something like @ "~" in C #)
"C:¥¥My Document¥¥fire¥¥test.txt"
To
r"C:¥My Document¥fire¥test.txt"
Can be written.
It seems that r may be capitalized.
R"C:¥My Document¥fire¥test.txt"
import re
m = re.match(r"FIRE[0-9]+", "FIRE123")
if m:
	print("matched")
else:
	print("Not matched")
Besides the match method, there is also a search method. The match method looks at whether it matches first, and the search method looks at whether it matches in the middle of the string.
m = re.match(r"FIRE[0-9]+", "Python FIRE123")     #Do not match
m = re.search(r"FIRE[0-9]+", "Python FIRE123")    #match
m = re.search(r"([a-z]+)\s*=\s*([0-9]+)", "index = 123")
if m:
	print(m.group(0))                # ->  "index = 123"
	print(m.group(1))                # ->  "index"
	print(m.group(2))                # ->  "123"
The named group is (? P 
m = re.search(r"(?P<name>[a-z]+)\s*=\s*(?P<value>[0-9]+)", "index = 123")
if m:
	print(m.group(0))                # ->  "index = 123"
	print(m.group(1))                # ->  "index"
	print(m.group(2))                # ->  "123"
	print(m.group('name'))           # ->  "index"
	print(m.group('value'))          # ->  "123"
m = re.match(r"abc([0-9]+).([0-9]+)", "abc123.456")
#---The whole match
print(m.start())                # -> 0
print(m.end())                  # ->10 * end is the next matched position (index)+The value of 1) is returned
print(m.span())                 # -> (0, 10)Returns start and end as tuples
#---Group 1
print(m.start(1))               # -> 3
print(m.end(1))                 # -> 6
print(m.span(1))                # -> (3, 6)
#---Group 2
print(m.start(2))               # -> 7
print(m.end(2))                 # -> 10
print(m.span(2))                # -> (7, 10)
reg = re.compile(r"ABC([0-9]+)")
m = reg.match("ABC888")
print(m.group(1))               # -> "888"
match_list = re.findall(r'([0-9]+)', "123a456b789")
print(match_list)               # -> ['123', '456', '789']
        Recommended Posts