我正在尝试将字符串中所有带连字符的单词替换为其单独的版本。我能够检测到带连字符的单词,但无法用单独的版本替换它们。我怎样才能做到这一点?
这是示例和示例代码:
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"
答案 0 :(得分:1)
re.sub()
,前瞻性和后瞻性:
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
答案 1 :(得分:1)
您可以使用一个非常简单的模式:
\b-\b
\b
字边界。-
连字符。\b
字边界。正则表达式演示here。
Python演示
import re
text = "one-hundered-and-three- some text foo-bar some--text"
print(re.sub(r'\b-\b', ' ', text))
打印:
one hundered and three- some text foo bar some--text
答案 2 :(得分:0)
您可以将re.sub()
与repl
参数一起使用:
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'
我在这里将它写为单行代码,但是我认为将lambda移到命名函数中会更清楚。