I'm trying to merge a hyphenated word in pythog. For example I have:
bla bla bla infor-
mation bla bla bla
I want to end up with blabla information bla bla bla.
I can match up the hyphen, remove the newline, all that stuff is easy, but I can't seem to get the two parts to join. The closes I got was bla bla bla infor mation bla bla, but obviously I can not match on a space without removing all other spaces.
Any help would be greatly appreciated.
Why would you ever use regex for such a simple task?
>>> s = """bla bla bla infor-
... mation bla bla bla"""
>>> s.replace('-\n','')
'bla bla bla information bla bla bla'
You can replace - with preceding and following space with an empty string: -
>>> import re
>>> s = "bla bla bla infor- mation bla bla bla"
>>> re.sub("\s*-\s*", "", s)
'bla bla bla information bla bla bla'
If you have new line at the end of hyphen: -
>>> s = """bla bla bla infor-
mation bla bla bla"""
>>> re.sub("\s*-\s*\n", "", s)
'bla bla bla information bla bla bla'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With