Match whole word in string including special characters

Question:

I am aware of multiple existing answers that suggest:

def contains(string, word):
    return bool(re.search(rf"b{word}b", string))

But this pattern gives special treatment to alphanumeric character. For examples, contains("hello world!", "world!") returns False while contains("hello world!", "world") returns True.

I need a more ‘naive’ search pattern, one that matches a substring as long as it starts and ends with either the superstring’s boundary or a space. (Desired behavior: opposite of the examples above.)

Asked By: Felix Fourcolor

||

Answers:

You need to avoid using b (word boundary) and assert that previous and next positions don’t have a non-whitespace character. Also it is safer to use re.escape as your search word may contain special regex meta characters.

You may use this python code:

def contains(string, word):
    return bool(re.search(rf"(?<!S){word}(?!S)", re.escape(string)))

print (contains("hello world!", "world"))
print (contains("hello world!", "world!"))

Output:

False
True

Online Code Demo

Answered By: anubhava
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.