为什么正则表达式搜索返回 None 相同的字符串?
Why is regex search returning None for the same string?
这段代码returnsNone,我做错了什么?我假设它与特殊字符有关
test=re.search('The Girl Who Played with Fire (Millennium #2)', 'The Girl Who Played with Fire (Millennium #2)',re.IGNORECASE)
print(test)
当字符串用作模式时,括号被视为分组运算符。
试试这个:
test=re.search(
'The Girl Who Played with Fire [(]Millennium #2[)]',
'The Girl Who Played with Fire (Millennium #2)',
re.IGNORECASE
)
您必须在正则表达式中转义 (
和 )
:
test=re.search(
'The Girl Who Played with Fire \(Millennium #2\)', # <- NOTICE
'The Girl Who Played with Fire (Millennium #2)',
re.IGNORECASE)
print(test)
这段代码returnsNone,我做错了什么?我假设它与特殊字符有关
test=re.search('The Girl Who Played with Fire (Millennium #2)', 'The Girl Who Played with Fire (Millennium #2)',re.IGNORECASE)
print(test)
当字符串用作模式时,括号被视为分组运算符。
试试这个:
test=re.search(
'The Girl Who Played with Fire [(]Millennium #2[)]',
'The Girl Who Played with Fire (Millennium #2)',
re.IGNORECASE
)
您必须在正则表达式中转义 (
和 )
:
test=re.search(
'The Girl Who Played with Fire \(Millennium #2\)', # <- NOTICE
'The Girl Who Played with Fire (Millennium #2)',
re.IGNORECASE)
print(test)