mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7mobile wallpaper 8mobile wallpaper 9mobile wallpaper 10mobile wallpaper 11mobile wallpaper 12mobile wallpaper 13
792 字
2 分鐘
正则表达式
2026-06-03

正则表达式#

正则表达式(Regular Expression,简称 regex 或 regexp)是一种用于匹配字符串中字符组合的模式。正则表达式在文本处理、数据验证、搜索和替换等场景中非常有用。它提供了一种强大的、灵活的语法来定义复杂的搜索模式。

正则表达式的基本组成部分#

  1. 字符

    • 匹配特定字符。例如,a 匹配字符 a
  2. 字符类

    • 使用方括号 [] 来定义一组字符,其中的任何一个字符都可以匹配。例如,[abc] 匹配字符 abc
    • 特殊字符类:
      • .:匹配任意单个字符(除了换行符)。
      • \d:匹配任意数字(相当于 [0-9])。
      • \D:匹配任意非数字(相当于 [^0-9])。
      • \w:匹配任意字母、数字或下划线(相当于 [a-zA-Z0-9_])。
      • \W:匹配任意非字母、数字或下划线(相当于 [^a-zA-Z0-9_])。
      • \s:匹配任意空白字符(包括空格、制表符、换行符等)。
      • \S:匹配任意非空白字符。
  3. 量词

    • 定义前面的字符或字符类出现的次数。
    • *:匹配前面的字符或字符类零次或多次。
    • +:匹配前面的字符或字符类一次或多次。
    • ?:匹配前面的字符或字符类零次或一次。
    • {n}:匹配前面的字符或字符类恰好 n 次。
    • {n,}:匹配前面的字符或字符类至少 n 次。
    • {n,m}:匹配前面的字符或字符类至少 n 次,但不超过 m 次。
  4. 锚点

    • 用于指定匹配的位置。
    • ^:匹配字符串的开始。
    • $:匹配字符串的结束。
    • \b:匹配单词边界。
    • \B:匹配非单词边界。
  5. 分组

    • 使用括号 () 来分组,可以对组进行量词操作。
    • 示例:(abc)+ 匹配 abc 一次或多次。
  6. 字符集

    • 使用 [] 来定义字符集,可以包含单个字符、字符范围或特殊字符类。
    • 示例:[a-z] 匹配任意小写字母。
  7. 特殊字符

    • 在正则表达式中,某些字符具有特殊含义,如 .*+?^$ 等。如果要匹配这些特殊字符本身,需要使用反斜杠 \ 进行转义。
    • 示例:\. 匹配字符 .

示例#

  1. 匹配特定字符串

    import re
    text = "Hello, world!"
    pattern = "world"
    match = re.search(pattern, text)
    if match:
    print("Match found:", match.group()) # 输出: Match found: world
    else:
    print("No match found")
  2. 使用字符类

    import re
    text = "Hello, 123 world!"
    pattern = r"\d+" # 匹配一个或多个数字
    match = re.search(pattern, text)
    if match:
    print("Match found:", match.group()) # 输出: Match found: 123
    else:
    print("No match found")
  3. 使用量词

    import re
    text = "Hello, world!"
    pattern = r"l+" # 匹配一个或多个 'l'
    match = re.search(pattern, text)
    if match:
    print("Match found:", match.group()) # 输出: Match found: ll
    else:
    print("No match found")
  4. 使用锚点

    import re
    text = "Hello, world!"
    pattern = r"^Hello" # 匹配字符串开头的 "Hello"
    match = re.search(pattern, text)
    if match:
    print("Match found:", match.group()) # 输出: Match found: Hello
    else:
    print("No match found")
  5. 使用分组

    import re
    text = "Hello, world!"
    pattern = r"(Hello), (\w+)" # 匹配 "Hello" 和一个单词
    match = re.search(pattern, text)
    if match:
    print("Match found:", match.group(0)) # 输出: Match found: Hello, world
    print("Group 1:", match.group(1)) # 输出: Group 1: Hello
    print("Group 2:", match.group(2)) # 输出: Group 2: world
    else:
    print("No match found")
  6. 使用替换

    import re
    text = "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():根据正则表达式分割字符串。

常用正则表达式示例#

  1. 匹配电子邮件地址

    import re
    text = "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']
  2. 匹配电话号码

    import re
    text = "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']
分享

如果這篇文章對你有幫助,歡迎分享給更多人!

正则表达式
https://lemusakuya.com/posts/study-notes/web-crawler/正则表达式/
作者
レム・咲く夜
發布於
2026-06-03
許可協議
CC BY-NC-SA 4.0

部分資訊可能已經過時

目錄