当前位置:   article > 正文

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

python正则匹配字符串

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)  # ['$']
```

以上就是Python正则表达式的用法详解,并举例详细描述每一种匹配的用法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/113752
推荐阅读
相关标签
  

闽ICP备14008679号