I am trying to create a regex that would remove any word that either starts or ends with a hyphen (not both).
word1- -> remove
-word2 -> remove
sub-word ->keep
My attempt is the following:
def begin_end_hyphen_removal(line):
return re.sub(r"((\s+|^)(-[A-Za-z]+)(\s+|$))|((\s+|^)([A-Za-z]+-)(\s+|$))","",line)
However, when I try to apply it on the following lines:
here are some word sub-words -word1 word2- sub-word2 word3- -word4
-word5 example
word6-
word7-
another one -word8
-word9
I get the same input as output again.