正则表达式
import re
模式匹配:使用re.search()方法可以在字符串中搜索匹配的模式。该方法返回一个Match对象,如果找不到匹配则为None。
import re
text = "Hello, world!"
match = re.search("world", text)
if match:
print("Match found!")
else:
print("No match found.")
模式匹配所有实例:使用re.findall()方法可以查找字符串中所有匹配模式的实例,并返回一个列表。
text = "Hello, world! Hello, world!"
matches = re.findall("world", text)
print(matches)
# 输出:['world', 'world']
预定义模式:Python提供了一些预定义的模式,可以使用它们来匹配不同的文本模式。例如,re.I用于不区分大小写匹配,re.M用于多行匹配。
import re
text = "Hello, World! hello, world!"
# 不区分大小写匹配
match1 = re.search(r"hello", text, re.I)
print(match1)
# 输出:<re.Match object; span=(6, 11), match='hello'>
# 多行匹配
text_multi_lines = "Hello\nWorld!"
match2 = re.search(r"Hello", text_multi_lines, re.M)
print(match2)
# 输出:<re.Match object; span=(0, 5), match='Hello'>
特殊字符和元字符:正则表达式中使用一些特殊字符和元字符来描述要匹配的模式。例如,.用于匹配任意字符,*用于匹配前面的字符零次或多次,+用于匹配前面的字符一次或多次。
import re
text = "apple, orange, banana"
# 匹配任意字符(`.`)
matches = re.findall(r".", text)
print(matches)
# 输出:['a', 'p', 'p', 'l', 'e', ' ', 'o', 'r', 'a', 'n', 'g', 'e']
# 匹配零次或多次(`*`)
matches2 = re.findall(r"o*", text)
print(matches2)
# 输出:['', '', '', '', '', 'o', '', '', '', '', '', '']
# 匹配一次或多次(`+`)
matches3 = re.findall(r"o+", text)
print(matches3)
# 输出:['oo']
字符类:使用方括号指定字符类,以匹配一组字符中的一个。例如,[a-z]用于匹配任意小写字母。
import re
text = "Hello, world! hello, Python!"
# 匹配任意小写字母
matches = re.findall(r"[a-z]", text)
print(matches)
# 输出:['e', 'l', 'l', 'o', 'h', 'y', 't', 't', 'p', 'n']
转义字符:使用反斜杠\可以转义特殊字符和元字符,以匹配它们本身。例如,\d用于匹配数字字符。
import re
text = "123abc456def"
# 匹配数字字符
matches = re.findall(r"\d", text)
print(matches)
# 输出:['1', '2', '3', '4', '5', '6']
捕获组:使用括号可以将模式分组,并将匹配的文本保存为组。捕获组可以用于后续的替换操作。
import re
text = "apple, orange, banana"
# 使用捕获组匹配并替换'o'为'x'
new_text = re.sub(r"o", "x", text)
print(new_text)
# 输出:'axpxple, xrxxxxng xxxxxnxna'
替换操作:使用re.sub()方法可以将字符串中匹配的模式替换为指定的文本。例如,下面的代码将字符串中的"apple"替换为"orange"。
import re
text = "I have an apple."
new_text = re.sub("apple", "orange", text)
print(new_text)
# 输出:"I have an orange."
具体的正则表达式示例可以让我们更好的掌握。
匹配一个或多个数字:
import re
text = "123abc456def"
matches = re.findall("\d+", text)
print(matches)
# 输出:['123', '456']
这个例子中,\d匹配任意数字字符,+表示匹配一个或多个数字字符。
匹配手机号码:
import re
text = "My phone number is 123-456-7890"
match = re.search("\d{3}-\d{3}-\d{4}", text)
if match:
print("Valid phone number")
else:
print("Invalid phone number")
这个例子中,\d{3}匹配三个数字,-用于分隔三位数字。整个模式匹配一个三位数、一个三位数和一个四位数,它们之间用破折号分隔开。
匹配邮箱地址:
import re
text = "My email is [example@example.com](mailto:example@example.com)"
match = re.search("\w+@\w+\.\w+", text)
if match:
print("Valid email address")
else:
print("Invalid email address")
这个例子中,\w+匹配一个或多个字母数字字符,@用于分隔用户名和域名,.匹配一个点字符,\w+再次匹配一个或多个字母数字字符,表示顶级域名。
我不接任何广告,写文章是没有任何收入的,单纯是为了将我觉得有用的东西分享给你们大家,您的点赞,在看和转发分享是对我最好的鼓励。如果有任何疑问随时留言公众号。
- End -
https://mp.weixin.qq.com/s/6HS2Qx4NicXjWGeCTNfuSw

浙公网安备 33010602011771号