Reputation: 1824
I am trying to replace all the hyphenated words in string with their separated versions. I am able to detect hyphenated words but I could not replace them with seperate versions. How can I do that?
This is the example and a sample code :
import re
text = "one-hundered-and-three- some text foo-bar some--text"
re.findall(r'\w+(?:-\w+)+',text)
# returns: ['one-hundered-and-three', 'foo-bar']
# I want to modify text as follows:
# text_new = "one hundered and three- some text foo bar some--text"
Upvotes: 1
Views: 348
Reputation: 26074
You can use a really simple pattern:
\b-\b
\b
Word boundary.-
Hyphen.\b
Word boundary.Regex demo here.
Python demo:
import re
text = "one-hundered-and-three- some text foo-bar some--text"
print(re.sub(r'\b-\b', ' ', text))
Prints:
one hundered and three- some text foo bar some--text
Upvotes: 1
Reputation: 26039
re.sub()
with positive lookahead and lookbehind:
import re
text = "one-hundered-and-three- some text foo-bar some--text"
print(re.sub(r'(?<=\w)-(?=\w)', ' ', text))
# one hundered and three- some text foo bar some--text
Upvotes: 1
Reputation: 500327
You could use re.sub()
with a function for the repl
argument:
In [12]: re.sub(r'\w+(?:-\w+)+', lambda match: match.group(0).replace('-', ' '), text)
Out[12]: 'one hundered and three- some text foo bar some--text'
I wrote it as a one-liner here, but I think it would be clearer if the lambda were moved out into a named function.
Upvotes: 0