如何在 Python 中编写正则表达式来匹配 a 或 b?
pythonserver side programmingprogramming更新于 2025/7/4 9:16:17
以下代码使用正则表达式 '(a|b)' 来匹配给定 Python 字符串中的 a 或 b
我们还使用标志 re.I 在匹配时忽略 a 或 b 的大小写
示例
import re s = 'Bank of Baroda' print(re.findall(r'(a|b)',s, re.I))
输出
这给出了输出
['B', 'a', 'B', 'a', 'a']
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

