0

Is there an error in the way python handles '.' or '\b'? I'm not sure why this produces differing results.

import re

regex1 = r'\.?\b'
print bool(re.match(regex1, '.'))

regex2 = r'a?\b'
print bool(re.match(regex2, 'a'))

Output:

False

True

2 Answers 2

4

\b, word boundary, matches between word characters and non-word elements. As such, it will match between a word character like a and the end of the string, but not between a non-word character like . and end of string.

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

Comments

0

As geekosaur pointed out \b is merely a short way of writing

(?:(?<=\w)(?!\w)|(?<!\w)(?=\w))

In your case you may want to use

(?!\w)

or

(?!\S)

instead of \b.

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.