zwlayer
zwlayer

Reputation: 1824

how to replace all hyphenated words

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

Answers (3)

Paolo
Paolo

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

Austin
Austin

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

NPE
NPE

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

Related Questions