You can use search queries:
(?:\b|(?<=_))word(?=\b|_)
^^^^^^^^^^^^^ ^^^^^^^
See regex demo , where (?:\b|(?<=_))is a non-captivating group matching a word boundary or a location preceded by _, and (?=\b|_)is a positive result matching either a word boundary or a character _.
Unfortunately, Python rewill not allow use (?<=\b|_), since the lookbehind template must have a fixed width (otherwise you will get an error look-behind requires fixed-width pattern).
A Python demo :
import re
rx = r"(?:\b|(?<=_))word(?=\b|_)"
s = "some_word_here and a word there"
print(re.findall(rx,s))
, (?<![^\W_])/(?![^\W_]) (. -):
rx = r"(?<![^\W_])word(?![^\W_])"
lookbehind (?<![^\W_]) , , -, _ char ( char _ ) (?![^\W_]) lookahead , char, - _ char (.. char _).