python 正则 删除固定字符
时间: 2023-04-06 19:02:37 浏览: 48
可以使用 re.sub() 函数来删除固定字符,例如:
import re
string = "hello, world! This is a test string."
pattern = "o"
new_string = re.sub(pattern, "", string)
print(new_string)
输出结果为:"hell, wrld! This is a test string."
相关问题
PYTHON 正则 删除固定字符
可以使用 re.sub() 函数来删除固定字符,例如:
```python
import re
string = "hello, world! the quick brown fox jumps over the lazy dog."
pattern = "the "
new_string = re.sub(pattern, "", string)
print(new_string)
```
输出结果为:
```
hello, world! quick brown fox jumps over lazy dog.
```
python正则匹配中文字符
使用 Python 正则表达式匹配中文字符可以使用 Unicode 编码范围进行匹配。
例如,匹配一个中文字符可以使用 `[\u4e00-\u9fa5]`,其中 `\u4e00` 和 `\u9fa5` 是 Unicode 编码范围,包含了所有的中文字符。
下面是一个例子:
```python
import re
text = "今天天气很好,出去玩吧!"
pattern = re.compile(r'[\u4e00-\u9fa5]')
result = pattern.findall(text)
print(result) # ['今', '天', '天', '气', '很', '好', '出', '去', '玩', '吧']
```
代码中使用 `re.compile()` 方法编译正则表达式,然后使用 `findall()` 方法查找匹配的中文字符。输出结果为一个列表,包含了所有的中文字符。