792 字
2 分鐘
正则表达式
正则表达式
正则表达式(Regular Expression,简称 regex 或 regexp)是一种用于匹配字符串中字符组合的模式。正则表达式在文本处理、数据验证、搜索和替换等场景中非常有用。它提供了一种强大的、灵活的语法来定义复杂的搜索模式。
正则表达式的基本组成部分
-
字符:
- 匹配特定字符。例如,
a匹配字符a。
- 匹配特定字符。例如,
-
字符类:
- 使用方括号
[]来定义一组字符,其中的任何一个字符都可以匹配。例如,[abc]匹配字符a、b或c。 - 特殊字符类:
.:匹配任意单个字符(除了换行符)。\d:匹配任意数字(相当于[0-9])。\D:匹配任意非数字(相当于[^0-9])。\w:匹配任意字母、数字或下划线(相当于[a-zA-Z0-9_])。\W:匹配任意非字母、数字或下划线(相当于[^a-zA-Z0-9_])。\s:匹配任意空白字符(包括空格、制表符、换行符等)。\S:匹配任意非空白字符。
- 使用方括号
-
量词:
- 定义前面的字符或字符类出现的次数。
*:匹配前面的字符或字符类零次或多次。+:匹配前面的字符或字符类一次或多次。?:匹配前面的字符或字符类零次或一次。{n}:匹配前面的字符或字符类恰好n次。{n,}:匹配前面的字符或字符类至少n次。{n,m}:匹配前面的字符或字符类至少n次,但不超过m次。
-
锚点:
- 用于指定匹配的位置。
^:匹配字符串的开始。$:匹配字符串的结束。\b:匹配单词边界。\B:匹配非单词边界。
-
分组:
- 使用括号
()来分组,可以对组进行量词操作。 - 示例:
(abc)+匹配abc一次或多次。
- 使用括号
-
字符集:
- 使用
[]来定义字符集,可以包含单个字符、字符范围或特殊字符类。 - 示例:
[a-z]匹配任意小写字母。
- 使用
-
特殊字符:
- 在正则表达式中,某些字符具有特殊含义,如
.、*、+、?、^、$等。如果要匹配这些特殊字符本身,需要使用反斜杠\进行转义。 - 示例:
\.匹配字符.。
- 在正则表达式中,某些字符具有特殊含义,如
示例
-
匹配特定字符串:
import retext = "Hello, world!"pattern = "world"match = re.search(pattern, text)if match:print("Match found:", match.group()) # 输出: Match found: worldelse:print("No match found") -
使用字符类:
import retext = "Hello, 123 world!"pattern = r"\d+" # 匹配一个或多个数字match = re.search(pattern, text)if match:print("Match found:", match.group()) # 输出: Match found: 123else:print("No match found") -
使用量词:
import retext = "Hello, world!"pattern = r"l+" # 匹配一个或多个 'l'match = re.search(pattern, text)if match:print("Match found:", match.group()) # 输出: Match found: llelse:print("No match found") -
使用锚点:
import retext = "Hello, world!"pattern = r"^Hello" # 匹配字符串开头的 "Hello"match = re.search(pattern, text)if match:print("Match found:", match.group()) # 输出: Match found: Helloelse:print("No match found") -
使用分组:
import retext = "Hello, world!"pattern = r"(Hello), (\w+)" # 匹配 "Hello" 和一个单词match = re.search(pattern, text)if match:print("Match found:", match.group(0)) # 输出: Match found: Hello, worldprint("Group 1:", match.group(1)) # 输出: Group 1: Helloprint("Group 2:", match.group(2)) # 输出: Group 2: worldelse:print("No match found") -
使用替换:
import retext = "Hello, world!"pattern = r"world"replacement = "Python"new_text = re.sub(pattern, replacement, text)print(new_text) # 输出: Hello, Python!
正则表达式模块
在 Python 中,正则表达式主要通过 re 模块来使用。re 模块提供了多种函数来处理正则表达式,例如:
re.search():在字符串中查找匹配的子串。re.match():从字符串的开头开始匹配。re.findall():查找所有匹配的子串,返回列表。re.sub():替换所有匹配的子串。re.split():根据正则表达式分割字符串。
常用正则表达式示例
-
匹配电子邮件地址:
import retext = "Contact us at info@example.com or support@example.org"pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"matches = re.findall(pattern, text)print(matches) # 输出: ['info@example.com', 'support@example.org'] -
匹配电话号码:
import retext = "Call me at 123-456-7890 or 987.654.3210"pattern = r"\b\d{3}[-.]\d{3}[-.]\d{4}\b"matches = re.findall(pattern, text)print(matches) # 输出: ['123-456-7890', '987.654.3210']
分享
如果這篇文章對你有幫助,歡迎分享給更多人!
部分資訊可能已經過時
相關文章 智能推薦
1
综合训练 对拍 面试表达
Algorithm 《算法》学习笔记:12_综合训练_对拍_面试表达
2
训练技巧 正则化 泛化与评估
Neural Networks 《神经网络》学习笔记:05_训练技巧_正则化_泛化与评估
3
AD Rush
Android Development 《Android Development》学习笔记:AD Rush
4
Chapt5
Digital Image Processing 《Digital Image Processing》学习笔记:Chapt5
5
Chapt6
Digital Image Processing 《Digital Image Processing》学习笔记:Chapt6





















