1. 正则表达式概述
正则表达式(Regular Expression,简称Regex)是一种用于匹配字符串中字符组合的模式。它是一种强大、灵活且高效的文本处理工具,广泛应用于数据验证、数据抽取、文本搜索和替换等场景。
1.1 模式匹配
正则表达式描述了一种字符串匹配的“模式”,可以用来检查一个串是否含有“某种”子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。
精准匹配
:例如,select from blog where title = ‘python’;
模糊匹配
:例如,select from blog where title like ‘%python%’;
正则表达式并不是Python所特有的,在Java、PHP、Go以及JavaScript等语言中都是支持正则表达式的。
1.2 正则表达式的功能
模式匹配
:根据指定的模式在文本中查找匹配项。
验证输入
:检查用户的输入是否符合预定义的格式,如邮箱地址、电话号码等。
2. 元字符
Python正则表达式中的元字符及其用途如下:
点号 (.)
:匹配除换行符之外的任意单个字符。
import re
pattern = r’p.y.t.h.o.n’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
匹配任何字母数字字符
:等同于
.
,它包括了所有小写和大写字母、数字以及下划线。
pattern = r’\w+’
string = ‘python1_2′
matches = re.match(pattern, string)
print(matches.group()) # 输出: python1_2
限定符
:
问号 (?)
:匹配前面的子表达式零次或一次。
pattern = r’py?thon’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
加号 (+)
:匹配前面的子表达式一次或多次。
pattern = r’py+thon’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
星号 (*)
:匹配前面的子表达式零次或多次。
pattern = r’py*thon’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
n (花括号内为n)
:匹配确定的n次。
pattern = r’py{2}thon’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
n, (花括号内为n逗号)
:匹配n次或更多次。
pattern = r’py{2,}thon’
string = ‘pyython’
matches = re.match(pattern, string)
print(matches.group()) # 输出: pyython
n,m (花括号内为n逗号m)
:匹配n到m次。
pattern = r’py{2,3}thon’
string = ‘pyython’
matches = re.match(pattern, string)
print(matches.group()) # 输出: python
区间字符 [ ]
:匹配指定范围内的任意字符。
pattern = r'[a-z]’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: p
排除字符
:使用
^
符号来表示排除指定范围内的字符。
pattern = r'[^a-z]’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: y
选择字符
:使用
|
符号来表示匹配多个选项中的任意一个。
pattern = r’py|pyt’
string = ‘python’
matches = re.match(pattern, string)
print(matches.group()) # 输出: py
转义字符
:使用
\\
符号来转义特殊字符。
pattern = r’\d+’
string = ‘123abc’
matches = re.match(pattern, string)
print(matches.group()) # 输出: 123
汉字匹配 [u4e00-u9fa5]
:匹配指定范围内的汉字。
pattern = r'[u4e00-u9fa5]+’
string = ‘你好世界’
matches = re.match(pattern, string)
print(matches.group()) # 输出: 你好世界
3. re模块
Python的
re
模块提供了对正则表达式的支持,其中包括以下主要函数:
match()
:从字符串的起始位置匹配正则表达式。
search()
:在字符串中搜索正则表达式。
findall()
:找到所有匹配的子串。
sub()
:替换匹配的子串。
split()
:将字符串分割成列表。
3.1 匹配字符
import re
pattern = r’\d+’
string = ‘123abc’
matches = re.match(pattern, string)
if matches:
print(matches.group()) # 输出: 123
3.2 匹配对象
import re
pattern = r’\d+’
string = ‘123abc’
matches = re.match(pattern, string)
if matches:
print(matches.group()) # 输出: 123
print(matches.start()) # 输出: 0
print(matches.end()) # 输出: 3
3.3 匹配标志
re.I
:忽略大小写。
re.M
:多行模式。
re.S
:点号匹配任何字符(包括换行符)。
import re
pattern = r’hello’
string = ‘Hello World!’
matches = re.match(pattern, string, re.I)
if matches:
print(matches.group()) # 输出: Hello
4. 实操
4.1 match函数的使用
import re
pattern = r’\d+’
string = ‘123abc’
matches = re.match(pattern, string)
if matches:
print(matches.group()) # 输出: 123
4.2 search函数的使用
import re
pattern = r’\d+’
string = ‘abc123abc’
matches = re.search(pattern, string)
if matches:
print(matches.group()) # 输出: 123
4.3 findall函数的使用
import re
pattern = r’\d+’
string = ‘abc123abc’
matches = re.findall(pattern, string)
print(matches) # 输出: [‘123’, ‘123’]
4.4 sub函数使用
import re
pattern = r’\d+’
string = ‘abc123abc’
new_string = re.sub(pattern, ‘0’, string)
print(new_string) # 输出: abc0abc
4.5 split函数使用
import re
pattern = r’\d+’
string = ‘abc123abc’
new_string = re.split(pattern, string)
print(new_string) # 输出: [‘abc’, ‘abc’, ”]
5. 总结
正则表达式是一种强大的文本处理工具,通过掌握选择与匹配技巧,我们可以轻松地处理各种文本问题。本文介绍了正则表达式的概述、元字符、re模块以及一些实用函数,希望对您有所帮助。