Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge hyphenated word in python

Tags:

python

regex

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.

like image 528
Kevin Avatar asked Apr 22 '26 05:04

Kevin


2 Answers

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'
like image 171
Demian Brecht Avatar answered Apr 24 '26 19:04

Demian Brecht


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'
like image 29
Rohit Jain Avatar answered Apr 24 '26 19:04

Rohit Jain