网站首页 > 基础教程 正文
Python 的正则表达式(Regular Expressions,简称 regex)是通过 re 模块来实现的。正则表达式是一种强大的文本处理工具,可以用来进行搜索、替换、匹配等操作。以下是一些常用的 re 模块功能及其示例:
1.导入re模块
import re
2. 编写正则表达式
正则表达式是一种模式描述语言,用于匹配字符串中的字符组合。在Python中,正则表达式通常作为字符串传递给re模块的函数。
3. 使用re模块的函数
re模块提供了多个函数来处理正则表达式,其中最常用的包括:
- re.match(pattern, string, flags=0): 尝试从字符串的起始位置匹配正则表达式。
- re.search(pattern, string, flags=0): 扫描字符串,返回第一个匹配正则表达式的子串的Match对象。
- re.findall(pattern, string, flags=0): 返回字符串中所有匹配正则表达式的子串的列表。
- re.sub(pattern, repl, string, count=0, flags=0): 使用repl替换字符串中所有匹配正则表达式的子串。
- re.compile(pattern, flags=0): 编译一个正则表达式模式,返回一个Pattern对象。
4. 处理匹配结果
如果匹配成功,re.match()、re.search()和re.finditer()会返回一个Match对象,你可以使用这个对象来获取匹配的详细信息,比如匹配的子串、匹配的位置等。
基本匹配
1. 匹配字符串
pattern = r'hello'
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
else:
print('No match found')
2. 匹配数字
pattern = r'\d+'
text = 'There are 123 apples'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
else:
print('No match found')
特殊字符
1..匹配任意字符(除了换行符)
pattern = r'a.c'
text = 'abc abc123'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.^匹配字符串的开始
python
复制代码
pattern = r'^hello'
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
3.$匹配字符串的结束
python
复制代码
pattern = r'world#39;
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
字符集
1.[abc]匹配方括号内的任意一个字符
python
复制代码
pattern = r'[abc]'
text = 'abc def'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.[a-z]匹配方括号内的字符范围
python
复制代码
pattern = r'[a-z]'
text = 'A quick brown fox'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
量词
1.*匹配前面的字符零次或多次
python
复制代码
pattern = r'a*b'
text = 'aaab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.+匹配前面的字符一次或多次
python
复制代码
pattern = r'a+b'
text = 'aab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
3.?匹配前面的字符零次或一次
python
复制代码
pattern = r'a?b'
text = 'ab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
4.{n}匹配前面的字符恰好 n 次
python
复制代码
pattern = r'a{3}b'
text = 'aaab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
分组和捕获
1. 使用圆括号进行分组
python
复制代码
pattern = r'(\d+)-(\d+)-(\d+)'
text = 'My birthday is 1990-01-01'
match = re.search(pattern, text)
if match:
print('Match found:', match.group(0)) # 整个匹配
print('Year:', match.group(1))
print('Month:', match.group(2))
print('Day:', match.group(3))
替换
1. 使用re.sub进行替换
python
复制代码
pattern = r'\d+'
text = 'There are 123 apples and 456 oranges'
new_text = re.sub(pattern, 'XXX', text)
print(new_text) # 输出: There are XXX apples and XXX oranges
编译正则表达式
1. 使用re.compile编译正则表达式
python
复制代码
pattern = re.compile(r'\d+')
text = 'There are 123 apples'
match = pattern.search(text)
if match:
print('Match found:', match.group())
查找所有匹配项
1. 使用re.findall查找所有匹配项
python
复制代码
pattern = r'\d+'
text = 'There are 123 apples and 456 oranges'
matches = re.findall(pattern, text)
print(matches) # 输出: ['123', '456']
忽略大小写
1. 使用re.IGNORECASE忽略大小写
python
复制代码
pattern = r'hello'
text = 'Hello World'
match = re.search(pattern, text, re.IGNORECASE)
if match:
print('Match found:', match.group())
多行匹配
1. 使用re.MULTILINE进行多行匹配
python
复制代码
pattern = r'^hello'
text = '''hello
world
hello again'''
matches = re.findall(pattern, text, re.MULTILINE)
print(matches) # 输出: ['hello', 'hello']
猜你喜欢
- 2024-10-12 python代码是如何执行的? python的代码如何运行
- 2024-10-12 deepin os下使用Geany编写python程序
- 2024-10-12 扩展和嵌入 Python 之重定向输出与编译
- 2024-10-12 python爬虫数据匹配-正则(re模块的用法)
- 2024-10-12 Python中的.pyc文件是干什么的呢?
- 2024-10-12 解锁 Python 中的正则表达式:轻松搞定文本处理
- 2024-10-12 Python 的底层 — 解释器和内存管理,你了解多少
- 2024-10-12 python程序执行原理 描述python程序执行原理
- 2024-10-12 这些 python 技巧,不知道就 out 了
- 2024-10-12 “挑战用 500 行 Python 写一个 C 编译器”
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)