正则表达式详解和举例说明

当涉及到正则表达式时,有很多公式可以用来匹配特定的模式。下面是一些常用的正则表达式公式及其解释:

1. 字母和数字的匹配:

- `\w`:匹配任何字母、数字或下划线字符。
- `\d`:匹配任何数字字符。
- `\s`:匹配任何空白字符。

例子:

import re

# 匹配所有字母、数字或下划线字符
pattern1 = r"\w+"
text1 = "Hello123_world!"
result1 = re.findall(pattern1, text1)
print(result1)  # 输出:['Hello123_world']

# 匹配所有数字字符
pattern2 = r"\d+"
text2 = "Hello123_world!"
result2 = re.findall(pattern2, text2)
print(result2)  # 输出:['123']

# 匹配所有空白字符
pattern3 = r"\s+"
text3 = "Hello 123_world!"
result3 = re.findall(pattern3, text3)
print(result3)  # 输出:[' ']

2. 重复数量:

- `+`:匹配前面的模式一次或多次。
- `*`:匹配前面的模式零次或多次。
- `?`:匹配前面的模式零次或一次。
- `{n}`:匹配前面的模式恰好 n 次。
- `{n,}`:匹配前面的模式至少 n 次。
- `{n,m}`:匹配前面的模式至少 n 次且不超过 m 次。

例子:

import re

# 匹配一个或多个连续的数字字符
pattern1 = r"\d+"
text1 = "Hello 123_world!"
result1 = re.findall(pattern1, text1)
print(result1)  # 输出:['123']

# 匹配零个或多个连续的字母字符
pattern2 = r"\w*"
text2 = "Hello 123_world!"
result2 = re.findall(pattern2, text2)
print(result2)  # 输出:['Hello', '', '123', '', 'world', '']

# 匹配零个或一个连续的字母字符
pattern3 = r"\w?"
text3 = "Hello 123_world!"
result3 = re.findall(pattern3, text3)
print(result3)  # 输出:['H', 'e', 'l', 'l', 'o', '', '1', '2', '3', '', 'w', 'o', 'r', 'l', 'd', '']

# 匹配恰好 3 次连续的字母字符
pattern4 = r"\w{3}"
text4 = "Hello 123_world!"
result4 = re.findall(pattern4, text4)
print(result4)  # 输出:['Hel', '123', 'wor']

# 匹配至少 2 次连续的字母字符
pattern5 = r"\w{2,}"
text5 = "Hello 123_world!"
result5 = re.findall(pattern5, text5)
print(result5)  # 输出:['Hello', '123', 'world']

# 匹配至少 2 次但不超过 3 次连续的字母字符
pattern6 = r"\w{2,3}"
text6 = "Hello 123_world!"
result6 = re.findall(pattern6, text6)
print(result6)  # 输出:['Hel', 'lo', '123', 'wor', 'ld']

3. 字符类:

- `[abc]`:匹配 a、b 或 c 中的任何一个字符。
- `[^abc]`:匹配除了 a、b 和 c 之外的任何字符。
- `[a-z]`:匹配任何小写字母。
- `[A-Z]`:匹配任何大写字母。
- `[0-9]`:匹配任何数字。

例子:

import re

# 匹配 a、b 或 c 中的任何一个字符
pattern1 = r"[abc]"
text1 = "Hello abc123_world!"
result1 = re.findall(pattern1, text1)
print(result1)  # 输出:['a', 'b', 'c', 'c']

# 匹配除了 a、b 和 c 之外的任何字符
pattern2 = r"[^abc]"
text2 = "Hello abc123_world!"
result2 = re.findall(pattern2, text2)
print(result2)  # 输出:['H', 'e', 'l', 'l', 'o', ' ', '1', '2', '3', '_', 'w', 'o', 'r', 'l', 'd', '!']

# 匹配任何小写字母
pattern3 = r"[a-z]"
text3 = "Hello abc123_world!"
result3 = re.findall(pattern3, text3)
print(result3)  # 输出:['e', 'l', 'l', 'o', 'a', 'b', 'c', 'w', 'o', 'r', 'l', 'd']

# 匹配任何大写字母
pattern4 = r"[A-Z]"
text4 = "Hello abc123_world!"
result4 = re.findall(pattern4, text4)
print(result4)  # 输出:['H']

# 匹配任何数字
pattern5 = r"[0-9]"
text5 = "Hello abc123_world!"
result5 = re.findall(pattern5, text5)
print(result5)  # 输出:['1', '2', '3']

4. 边界:

- `^`:匹配字符串的开头。
- `$`:匹配字符串的结尾。
- `\b`:匹配单词的边界。

例子:

import re

# 匹配以 Hello 开头的字符串
pattern1 = r"^Hello"
text1 = "Hello abc123_world!"
result1 = re.findall(pattern1, text1)
print(result1)  # 输出:['Hello']

# 匹配以 world! 结尾的字符串
pattern2 = r"world!$"
text2 = "Hello abc123_world!"
result2 = re.findall(pattern2, text2)
print(result2)  # 输出:['world!']

# 匹配包含单词 abc 的字符串
pattern3 = r"\babc\b"
text3 = "Hello abc123_world!"
result3 = re.findall(pattern3, text3)
print(result3)  # 输出:['abc']

5. 分组和捕获:

- `()`:将模式分组。
- `?:`:指定一个非捕获组。

例子:

import re

# 匹配重复的单词
pattern1 = r"\b(\w+)\s+\1\b"
text1 = "Hello Hello world world!"
result1 = re.findall(pattern1, text1)
print(result1)  # 输出:['Hello', 'world']

# 匹配重复的单词但不捕获分组
pattern2 = r"\b(?:\w+)\s+\1\b"
text2 = "Hello Hello world world!"
result2 = re.findall(pattern2, text2)
print(result2)  # 输出:['Hello world']

这些只是一些常用的正则表达式公式,还有其他更高级和复杂的公式可以用来匹配更特定的模式。如果你想进一步学习正则表达式,我建议你查阅 Python 的 re 模块文档,其中详细解释了各种正则表达式的用法和语法。

希望这些例子能帮助你更好地理解正则表达式的公式!

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值