whi
whi

Reputation: 2750

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?

Upvotes: 13

Views: 44130

Answers (3)

kampu
kampu

Reputation: 1421

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

Upvotes: 27

Mark Reed
Mark Reed

Reputation: 95297

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?

Upvotes: 0

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.

Upvotes: 0

Related Questions