引言

正则表达式(Regular Expression,简称 Regex 或 Regexp)是一种用于处理字符串的强大工具,广泛应用于文本编辑、编程、数据验证等领域。它能够帮助我们高效地匹配、查找、替换和验证字符串模式。本文将带你从正则表达式的入门知识开始,逐步深入,最终通过实战案例,帮助你全面掌握正则表达式的奥秘。

第一部分:正则表达式基础

1. 什么是正则表达式?

正则表达式是一种用于描述字符串模式的语法规则。它允许开发者定义一个模式,然后使用这个模式来匹配、查找、替换或验证字符串。

2. 正则表达式的组成

正则表达式由字符、元字符和量词组成。

  • 字符:包括字母、数字、标点符号等。
  • 元字符:具有特殊含义的符号,如 .*+? 等。
  • 量词:用于指定匹配的次数,如 * 表示零次或多次,+ 表示一次或多次。

3. 正则表达式的执行过程

正则表达式的执行过程分为两个阶段:编译和匹配。

  • 编译:将正则表达式编译成内部表示形式。
  • 匹配:使用编译后的内部表示形式对文本进行匹配。

第二部分:常用正则表达式元字符

1. 点号(.)

点号(.)匹配除换行符以外的任意单个字符。

import re

pattern = r'.*world.*'
text = 'Hello world! Have a good world.'

match = re.match(pattern, text)
if match:
    print("Match found:", match.group())
else:
    print("No match found.")

2. 星号(*)

星号(*)匹配前面的子表达式零次或多次。

pattern = r'\b\w*\b'
text = 'This is a test string with multiple words.'

matches = re.findall(pattern, text)
print("Matches:", matches)

3. 加号(+)

加号(+)匹配前面的子表达式一次或多次。

pattern = r'\b\w+\b'
text = 'This is a test string with multiple words.'

matches = re.findall(pattern, text)
print("Matches:", matches)

4. 问号(?)

问号(?)匹配前面的子表达式零次或一次。

pattern = r'\b\w?\b'
text = 'This is a test string with single and multiple words.'

matches = re.findall(pattern, text)
print("Matches:", matches)

5. 花括号({})

花括号({})用于指定匹配的次数。

pattern = r'\b\w{3}\b'
text = 'This is a test string with three-word patterns.'

matches = re.findall(pattern, text)
print("Matches:", matches)

6. 方括号([])

方括号([])用于匹配方括号内的任意一个字符。

pattern = r'\b[a-z]+\b'
text = 'This is a test string with lowercase patterns.'

matches = re.findall(pattern, text)
print("Matches:", matches)

7. 脱字符(^)

脱字符(^)匹配字符串的开始。

pattern = r'^Hello'
text = 'Hello world!'

match = re.match(pattern, text)
if match:
    print("Match found:", match.group())
else:
    print("No match found.")

8. 美元符号($)

美元符号($)匹配字符串的结束。

pattern = r'world$'
text = 'Hello world!'

match = re.match(pattern, text)
if match:
    print("Match found:", match.group())
else:
    print("No match found.")

第三部分:高级正则表达式技巧

1. 捕获组

捕获组用于捕获匹配的子表达式。

pattern = r'\b(\w+)\b(\s+\1\b)'
text = 'This is a test string with multiple words test.'

matches = re.findall(pattern, text)
print("Matches:", matches)

2. 反向引用

反向引用允许我们在正则表达式中引用之前匹配的捕获组。

pattern = r'\b(\w+)\b(\s+\1\b)'
text = 'This is a test string with multiple words test.'

matches = re.findall(pattern, text)
print("Matches:", matches)

3. 非捕获组

非捕获组用于匹配但不保存匹配的子表达式