如何用re.sub替换部分字符串

时间:2017-12-18 17:11:32

标签: python regex python-3.x

假设我有文字:

text = 'Washington state Washington DC"

我希望我的新输出

'WA state Washington DC'

我试过

re.sub('Washington(\s[^DC])', 'WA ', text)

并获取以下输出,删除“state”的第一个字母:

'WA tate Washington DC'

基本上,我希望“华盛顿”的每个实例都改为“WA”,只要它不在“DC”之前。我敢肯定有一种非常简单的方法可以做到这一点,我的大脑就像今天不能工作一样! (我正在使用Python 3.x)请帮忙!感谢

3 个答案:

答案 0 :(得分:4)

使用像这样的否定前瞻:

Washington(?!\s*DC)

它将检查华盛顿是否有任意数量的空格和" DC"

答案 1 :(得分:0)

感谢您提出问题。它让我磨练了我相对较新的Python技能。有很多方法可以做到这一点。我喜欢这样:

import re

wa = "Washington state Washington DC"

regexp  = r'Washington\s'
regexp1 = r'WA(\s+DC)'
text    = re.sub(regexp, 'WA ', wa)
text2   = re.sub(regexp1, 'Washington DC', text)
print(text2)

基本上,它改变了华盛顿'华盛顿的所有事件。到' WA'然后改变所有出现的& WA DC'到华盛顿特区'。

答案 2 :(得分:0)

你可以试试这个:

import re
text = ["Washington state Washington DC", "the great state of Washington", "Lives in Washington DC", "I live in Washington State"]
new_text = [re.sub('Washington(?!\sDC)', 'WA', i) for i in text]

输出:

['WA state Washington DC', 'the great state of WA', 'Lives in Washington DC', 'I live in WA State']

测试用例:

text = {"Washington state Washington DC":"WA state Washington DC", "the great state of Washington":"the great state of WA", "Lives in Washington DC":"Lives in Washington DC", "I live in Washington State":"I live in WA State"}
for a, b in text.items():
   assert re.sub('Washington(?!\sDC)', 'WA', a) == b, "failed"
print("passed")

输出:

passed