Python regex ignorecase不工作

2024-04-27 05:03:27 发布

您现在位置:Python中文网/ 问答频道 /正文

#!/usr/bin/python

import re

regex = re.compile('B', re.IGNORECASE)
print regex.match('abc')

当它应该匹配时返回None。我尝试使用regex'B'和'abc'的搜索字符串,并忽略大小写


Tags: 字符串importrenonebinusrmatchregex
1条回答
网友
1楼 · 发布于 2024-04-27 05:03:27

^{}尝试从输入字符串的开始进行匹配。在

您似乎需要^{},它将扫描输入字符串以查找匹配项。在

import re
regex = re.compile('B', re.IGNORECASE)
m = regex.search('abc')
if m:
    print m.group(0)

输出:

^{pr2}$

相关问题 更多 >