• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python正则表达式字符匹配详细用法和举例说明

武飞扬头像
爱学小野兽
帮助1

Python正则表达式是一种强大的文本处理工具,可以用来匹配、搜索、替换文本中的特定模式。下面是Python正则表达式的用法详解:

1. 匹配单个字符

- .:匹配任意一个字符,除了换行符
- []:匹配括号中任意一个字符
- [^]:匹配不在括号中的任意一个字符
- \d:匹配任意一个数字
- \D:匹配任意一个非数字字符
- \w:匹配任意一个字母、数字或下划线
- \W:匹配任意一个非字母、数字或下划线字符
- \s:匹配任意一个空白字符
- \S:匹配任意一个非空白字符

例如:

```python
import re

# 匹配任意一个字符
pattern = r'.'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

# 匹配数字
pattern = r'\d'
text = 'abc123def456'
result = re.findall(pattern, text)
print(result)  # ['1', '2', '3', '4', '5', '6']

# 匹配非数字字符
pattern = r'\D'
text = 'abc123def456'
result = re.findall(pattern, text)
print(result)  # ['a', 'b', 'c', 'd', 'e', 'f']

# 匹配字母、数字或下划线
pattern = r'\w'
text = 'hello_world123'
result = re.findall(pattern, text)
print(result)  # ['h', 'e', 'l', 'l', 'o', '_', 'w', 'o', 'r', 'l', 'd', '1', '2', '3']

# 匹配非字母、数字或下划线字符
pattern = r'\W'
text = 'hello_world123'
result = re.findall(pattern, text)
print(result)  # []

# 匹配空白字符
pattern = r'\s'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # [' ']

# 匹配非空白字符
pattern = r'\S'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
```

2. 匹配多个字符

- *:匹配前面的字符0次或多次
- :匹配前面的字符1次或多次
- ?:匹配前面的字符0次或1次
- {n}:匹配前面的字符恰好n次
- {n,}:匹配前面的字符至少n次
- {n,m}:匹配前面的字符至少n次,但不超过m次

例如:

```python
import re

# 匹配前面的字符0次或多次
pattern = r'ab*'
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['a', 'ab', 'abb', 'abbb', 'abbb']

# 匹配前面的字符1次或多次
pattern = r'ab '
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['ab', 'abb', 'abbb', 'abbb']

# 匹配前面的字符0次或1次
pattern = r'ab?'
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['a', 'ab', 'ab', 'ab', 'ab']

# 匹配前面的字符恰好n次
pattern = r'ab{2}'
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['abb']

# 匹配前面的字符至少n次
pattern = r'ab{2,}'
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['abb', 'abbb', 'abbb']

# 匹配前面的字符至少n次,但不超过m次
pattern = r'ab{2,3}'
text = 'a ab abb abbb abbbb'
result = re.findall(pattern, text)
print(result)  # ['abb', 'abbb', 'abbb']
```

3. 匹配开头和结尾

- ^:匹配字符串的开头
- $:匹配字符串的结尾

例如:

```python
import re

# 匹配字符串的开头
pattern = r'^hello'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # ['hello']

# 匹配字符串的结尾
pattern = r'world$'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # ['world']
```

4. 匹配分组

- ():将括号中的内容作为一个分组
- |:匹配多个模式中的任意一个

例如:

```python
import re

# 将括号中的内容作为一个分组
pattern = r'(hello) (world)'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # [('hello', 'world')]

# 匹配多个模式中的任意一个
pattern = r'hello|world'
text = 'hello world'
result = re.findall(pattern, text)
print(result)  # ['hello', 'world']
```

5. 匹配转义字符

- \:转义字符

例如:

```python
import re

# 匹配转义字符
pattern = r'\$'
text = 'The price is $10.'
result = re.findall(pattern, text)
print(result)  # ['$']
```

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhahhbe
系列文章
更多 icon
同类精品
更多 icon
继续加载