正则表达式'^ [abc] + $'不能正常工作

cae*_*sar 3 python regex pcre python-3.x

我想找到仅包含字母a,b和c的每一行。我有正则表达式

print(re.findall('^[abc]+$', text))
Run Code Online (Sandbox Code Playgroud)

但这段文字没有任何结果:

print(re.findall('^[abc]+$', text))
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我认为问题在于^$字符,但我不明白为什么。

For*_*Bru 5

您想查找仅包含这些字母的每一。因此,使用以下命令搜索行re.MULTILINE

print(re.findall('^[abc]+$', text, re.MULTILINE))
Run Code Online (Sandbox Code Playgroud)

如果没有此标志,re将视text作为一个单一的线,并^$将引用开头和结尾的文件的全部内容