简体   繁体   中英

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?

\\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".

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.

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.

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. It's a tautology. I suspect you are trying to check for something else?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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