DanielTheRocketMan
DanielTheRocketMan

Reputation: 3249

How to replace hyphen and newline in string in Python

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

Answers (3)

The fourth bird
The fourth bird

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)

Regex demo | Python demo

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

Paolo
Paolo

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

Anwarvic
Anwarvic

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

Related Questions