-1

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

Felix Fourcolor
  • 320
  • 2
  • 8

1 Answers1

2

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

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
anubhava
  • 761,203
  • 64
  • 569
  • 643