假设我有文字:
text = 'Washington state Washington DC"
我想要我的新输出
'WA state Washington DC'
我试过了
re.sub('Washington(\s[^DC])', 'WA ', text)
并获得以下输出,删除“州”的第一个字母:
'WA tate Washington DC'
基本上,我希望“华盛顿”的每个实例都改为“WA”,只要它不在“DC”之前。我敢肯定有一种非常简单的方法可以做到这一点,我的大脑就像今天不能工作一样! (我正在使用Python 3.x)请帮忙!谢谢
使用像这样的负向前瞻:
Washington(?!\s*DC)
它将检查华盛顿是否有任意数量的空格和“DC”
谢谢你的提问。它让我磨练了我相对较新的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”的所有出现改为“华盛顿特区”。
你可以试试这个:
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