How can I replace a hyphens in the middle of a word using python3 and re.sub()?
“-ice-cream- – hang-out” -> “-ice cream- – hang out”
Thanks,
Barry
EDIT: I tried
self.lines = re.sub(r'\w(-)\w', " ", self.lines)
but wasn’t sure how to proceed. I like the /b way of doing it.
re.sub(pattern, repl, string[, count, flags])see docs.python.orgYour pattern would be
r'\b-\b'See the pattern here on Regexr
And replace this with a space (
' ')The
rbefore the regex string deifnes a raw string, that means you don’t need double escaping.\bis a word boundary, that means it will match on a-when there is a word character before and after.