python正则表达式:匹配空格字符或字符串结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16519744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python regex: to match space character or end of string
提问by whi
I want to match space chars or end of string in a text.
我想在文本中匹配空格字符或字符串结尾。
import re
uname='abc'
assert re.findall('@%s\s*$' % uname, '@'+uname)
assert re.findall('@%s\s*$' % uname, '@'+uname+' '+'aa')
assert not re.findall('@%s\s*$' % uname, '@'+uname+'aa')
The pattern is not right.
How to use python?
图案不对。
如何使用蟒蛇?
采纳答案by kampu
\s*$is incorrect: this matches "zero or more spaces followed bythe end of the string", rather than "one or more spaces orthe end of the string".
\s*$不正确:这匹配“零个或多个空格后跟字符串结尾”,而不是“一个或多个空格或字符串结尾”。
For this situation, I would use
(?:\s+|$)(inside a raw string, as others have mentioned).
The (?:)part is just about separating that subexpression so that the | operator matches the correct fragment and no more than the correct fragment.
对于这种情况,我会使用
(?:\s+|$)(在原始字符串中,正如其他人提到的那样)。这(?:)部分只是关于分离该子表达式,以便 | 运算符匹配正确的片段,并且不超过正确的片段。
回答by StoryTeller - Unslander Monica
Try this:
尝试这个:
assert re.findall('@%s\s*$' % uname, '@'+uname)
You must escape the \character if you don't use raw strings.
\如果不使用原始字符串,则必须对字符进行转义。
It's a bit confusing, but stems from the fact that \is a meta character for both the python interpreter and the remodule.
这有点令人困惑,但源于这样\一个事实,即 python 解释器和re模块的元字符。
回答by Mark Reed
Use raw strings.
使用原始字符串。
assert re.findall(r'@%s\s*$' % uname, '@'+uname)
Otherwise the use of \as a special character in regular strings conflicts with its use as a special character in regular expressions.
否则,\在正则字符串中作为特殊字符的使用与其在正则表达式中作为特殊字符的使用相冲突。
But this assertion is impossible to fail. Of course, a string consisting of nothing but "@" plus the contents of the variable unameis going to match a regular expression of "@" plus unameplus optional (always empty) whitespace and then the end of the string. It's a tautology. I suspect you are trying to check for something else?
但这种说法是不可能失败的。当然,一个仅由“@”加上变量内容组成的字符串uname将匹配“@”uname加上可选(始终为空)空格的正则表达式,然后是字符串的结尾。这是一个同义反复。我怀疑您正在尝试检查其他内容?

