正则表达式 '^[abc]+$' 未按预期工作 [英] Regular expression '^[abc]+$' not working as intended

查看:38
本文介绍了正则表达式 '^[abc]+$' 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到仅由字母 a、b 和 c 组成的每一行.我有正则表达式

I want to find every line which consists only of letters a, b and c. I've got the regular expression

print(re.findall('^[abc]+$', text))

但我没有从这段文字中得到任何结果:

but I get no result back from this text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
fsadfasd
quis nostraud exercitatione ullamco laboiris nisi ut aloiquip ex ea commuodo consequat.
gfgfgasdas
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
aaaabbbbcccaabcccabc
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
aabcbcbcbbabbbabcbbcbcf
culpa qui ofaeiouficia deserunt mollit anim id est laborum.
bbcbcbcbcbcbcbcbcbcbcbcbcbc
aeiou
aaaaaaaaaaaaaaaaaaaaaaaa

这是为什么?我认为问题出在 ^$ 字符上,但我不明白为什么.

Why is this? I think the problem is with the ^ and $ characters, but I don't understand why.

推荐答案

您想找到仅由这些字母组成的每一行.因此,搜索带有 re.MULTILINE 的行:

You want to find every line that consists of only these letters. So, search over the lines with re.MULTILINE:

print(re.findall('^[abc]+$', text, re.MULTILINE))

如果没有这个标志,re 会将 text 视为一行,^$ 将引用到文件全部内容的开头和结尾.

Without this flag, re will treat text as a single line, and ^ and $ will refer to the beginning and end of the whole content of the file.

这篇关于正则表达式 '^[abc]+$' 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆