Search string for word

PHOTO EMBED

Tue Apr 04 2023 04:11:14 GMT+0000 (Coordinated Universal Time)

Saved by @tofufu #python

import re

# Target String
target_string = "Emma is a baseball player who was born on June 17, 1993."

# find substring 'ball'
result = re.search(r"ball", target_string)

# Print matching substring
print(result.group())
# output 'ball'

# find exact word/substring surrounded by word boundary
result = re.search(r"\bball\b", target_string)
if result:
    print(result)
# output None

# find word 'player'
result = re.search(r"\bplayer\b", target_string)
print(result.group())
# output 'player'
content_copyCOPY

https://pynative.com/python-regex-search/