我需要在test_string
:中替换-
import re
test_string = "refer- ences har- ness Stand- ard Re- quired www.mypo- rtal.test.com This - is a document"
re.sub(r"[- ]", "", test_string)
预期结果:
references harness Standard Required www.myportal.test.com This - is a document
基本上,只有当-
之前没有空白时,才应该进行替换。
我该怎么做?目前我得到的是:
referencesharnessStandardRequiredwww.myportal.test.comThisisadocument
我很感激有人也能解释解决方案(不仅仅是给出正确的正则表达式(
您可以使用
import re
test_string = "refer- ences har- ness Stand- ard Re- quired www.mypo- rtal.test.com This - is a document"
print(re.sub(r"(?<!s)-s+|", "", test_string))
# => references harness Standard Required www.myportal.test.com This - is a document
请参阅regex演示和Python演示。
详细信息
(?<!s)
-一个负查找,如果当前位置左侧有空白,则匹配失败-
-连字符s+
-一个或多个空白字符
import re
test_string = "refer- ences har- ness Stand- ard Re- quired www.mypo- rtal.test.com This - is a document"
test_string = re.sub(r'[a-zA-Z0-9]- ', '', test_string)
print(test_string)