import os
import re
from pathlib import Path, PurePath
使用match函数只能检测匹配的第一个字符
pattern = "s"
strs = "sdsjflakdhfpsa"
res = re.match(pattern, strs)
print(res)
print(res.group())
s
使用findall实现所有字符的匹配
res = re.findall(pattern, strs)
print(res)
['s', 's', 's']
元字符
print(re.match(".", "123456789"))
print(re.match("\d", "123456789"))
print(re.match("\D", "a_123456789"))
print(re.match("\s\s", "\n\t"))
print(re.match("\w\w\w\w", "_Aa123456789"))
可以使用范围, 一个中括号表示一个字符位置
print(re.match("[0-9][0-9]", "123456789"))
print(re.match("[a-e]", "asddfffgg"))
print(re.match("[w-z]", "xsfasdff"))
print(re.match("[0-9a-e]", "123456789"))
多字符的匹配
print(re.match("\d\d\d\d\d\d\d\d\d", "123456789"))
print(re.match("\d*", "123456789"))
print(re.match("\d*", "12345a6789"))
print(re.match("\d+", "12a3456789"))
print(re.match("\d+", "a12a3456789"))
None
次数匹配
print(re.match("\d{3}", "1234a3456789"))
print(re.match("\d{3,}", "1234a3456789"))
print(re.match("\d{3,6}", "1234345a6789"))
边界处理
tel = "13345678910aa298097"
print(re.match("^1[358][1-9]\d{8}$", tel))
str = "Welcome to Longman Dictionary of Contemporary English Online"
print(re.findall('an\\b', str))
print(re.findall('\\bLon', str))
None
['an']
['Lon']
分组匹配
t = "2023-10-30fagd"
print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t))
print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group())
print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(0))
print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(1))
print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(2))
print(re.match("(\d{4})-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(1))
2023-10-30
2023-10-30
10
30
2023