简体   繁体   English

python regex:匹配空格字符或字符串结尾

[英]python regex: to match space character or end of string

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? 如何使用python?

\\s*$ is incorrect: this matches "zero or more spaces followed by the end of the string", rather than "one or more spaces or the end of the string". \\s*$不正确:这匹配“零或多个空格后跟字符串的结尾”,而不是“一个或多个空格字符串的结尾”。

For this situation, I would use (?:\\s+|$) (inside a raw string, as others have mentioned). 对于这种情况,我会使用(?:\\s+|$) (在原始字符串中,正如其他人提到的那样)。 The (?:) part is just about separating that subexpression so that the | (?:)部分只是将子表达式分开,以便| operator matches the correct fragment and no more than the correct fragment. 运算符匹配正确的片段,而不是正确的片段。

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 re module. 这有点令人困惑,但源于这样的事实: \\是python解释器和re模块的元字符。

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 uname is going to match a regular expression of "@" plus uname plus optional (always empty) whitespace and then the end of the string. 当然,除了“@”加上变量uname的内容之外的字符串将匹配正则表达式“@”加上uname加上可选(总是空的)空格,然后匹配字符串的结尾。 It's a tautology. 这是一个重言式。 I suspect you are trying to check for something else? 我怀疑你正试图检查别的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM