Reputation: 3249
I am working in a text with several syllables divisions.
A typical string is something like that
"this good pe-
riod has"
I tried:
my_string.replace('-'+"\r","")
However, it is not working.
I would like to get
"this good period has"
Upvotes: 1
Views: 994
Reputation: 163287
It depends how your string ends, you could also use my_string.replace('-\r\n', '')
or an optional carriage return using re.sub and -(?:\r?\n|\r)
If there has to be a word character before and after, instead of removing all the hyphens at the end of the line, you could use lookarounds:
(?<=\w)-\r?\n(?=\w)
For example
import re
regex = r"(?<=\w)-\r?\n(?=\w)"
my_string = """this good pe-
riod has"""
print (re.sub(regex, "", my_string))
Output
this good period has
Upvotes: 1
Reputation: 26074
After you match -
, you should match the newline \n
:
my_string = """this good pe-
riod has"""
print(my_string.replace("-\n",""))
# this good period has
Upvotes: 1
Reputation: 12992
Have you tried this?
import re
text = """this good pe-
riod has"""
print(re.sub(r"-\s+", '', text))
# this good period has
Upvotes: 1