Python正则表达式忽略不起作用

Nic*_*ick 0 python regex

#!/usr/bin/python

import re

regex = re.compile('B', re.IGNORECASE)
print regex.match('abc')
Run Code Online (Sandbox Code Playgroud)

当它匹配时返回None.我正在尝试使用正则表达式'B'和搜索字符串'abc'与忽略大小写

Jon*_*art 5

re.match()尝试从输入字符串的开头匹配.

看来你想re.search(),它会扫描输入字符串寻找匹配.

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

输出:

b
Run Code Online (Sandbox Code Playgroud)