1. *表示匹配任意多个字符 \d*表示匹配任意多个数字字符
import re
text = "123h1ello world"
text1 = "123Hello world456"
text2 = "hello world"
res = re.match("\d*", text)
res1 = re.match("\d*", text1)
res2 = re.match("\d*", text2)
print(res.group())
print(res1.group())
print(res2.group())
输出结果为
123
123
Process finished with exit code 0
2. +表示匹配1个或者多个任意字符 \w+表示匹配一个或多个字母,数字或下划线
import re
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
res = re.match("\w+", text)
res1 = re.match("\w+", text1)
res2 = re.match("\w+", text2)
print(res.group())
print(res1.group())
print(res2)
输出结果为
he
Hello
None
Process finished with exit code 0
3. ?表示匹配0个或一个字符 \w?表示匹配0或1个字母,数字或下划线
import re
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
res = re.match("\w?", text)
res1 = re.match("\w?", text1)
res2 = re.match("\w?", text2)
print(res.group())
print(res1.group())
print(res2.group())
输出结果为
h
H
Process finished with exit code 0
3. {m}表示匹配m个字符 \d{11}表示匹配11个数字字符
import re
text = "he+llo world"
text1 = "Hello world456"
text2 = "hello world"
res = re.match("\w{2}", text)
res1 = re.match("\w{3}", text1)
res2 = re.match("\w{4}", text2)
print(res.group())
print(res1.group())
print(res2.group())
输出结果为
he
Hel
hell
Process finished with exit code 0
5. {m,n}表示匹配m-n个字符 \w{2,4}表示匹配2-4个字符
import re
text = "hello world"
text1 = "Helloworld456"
text2 = "hello world"
res = re.match("\w{2,5}", text)
res1 = re.match("\w{6,8}", text1)
res2 = re.match("\w{20,25}", text2)
print(res.group())
print(res1.group())
print(res2)
hello
Hellowor
None
Process finished with exit code 0