Python正则表达式匹配任何内容

bluesky1年前 ⋅ 155 阅读

ChatGPT的回答仅作参考:

可以使用正则表达式的通配符`.`来匹配任何字符。例如,使用`re`模块的`search`函数可以搜索字符串中的任何内容:

```python
import re

string = "Hello, World!"
pattern = "."

match = re.search(pattern, string)
if match:
    print("Match found:", match.group())
else:
    print("No match found.")
```

输出结果为:

```
Match found: H
```

在这个例子中,正则表达式`.`匹配了字符串中的第一个字符`H`。

全部评论: 0

    相关推荐