13

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?

3 Answers 3

27

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

sorry but second not passed
@whi, the second one didn't pass because @abc aa doesn't have match the pattern of white spaces followed by the end of the string. See Kampu's answer.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.