user253956
user253956

Reputation: 323

How to look only at entire words with regex?

I am writing personal information filter. When it encounters VALID phone or email replaces it with "[PRIVATE]";

Valid phone is for example '0123 45678' and '00123 45678' is invalid, but i get 0[PRIVATE] for the second one after the filtering. How do i look only at entire words using regex and \bword\b is totally not working properly.

Upvotes: 0

Views: 49

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 98991

This one will work:

re.search(r"([\d]+([\s]+)?[\d]+)")

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

I'm betting that you forgot to use raw strings:

re.search("\bword\b") 

finds a string that starts with a backspace character, then word, then another backspace character.

re.search(r"\bword\b") 

finds an entire word.

Upvotes: 5

Related Questions