例如,我有一个字符串“完美猎熊”,我想在“熊”出现之前用“the”替换这个单词。
所以最终的结果是“猎熊”
我想我会用
re.sub("\w+ bear","the","perfect bear hunts")
但它也取代了“熊”我如何排除熊被取代,同时也有它在匹配使用?
最佳答案
和其他答案一样,我会使用积极的前瞻性断言。
然后,通过在几条评论中拉扯来解决这个问题(比如“beard”这个词呢?),我将添加(\b|$)
这与单词边界或字符串结尾匹配,因此只匹配单词bear
,不再匹配。
所以你得到了以下信息:
import re
def bear_replace(string):
return re.sub(r"\w+ (?=bear(\b|$))", "the ", string)
和测试用例(使用pytest):
import pytest
@pytest.mark.parametrize('string, expected', [
("perfect bear swims", "the bear swims"),
# We only capture the first word before 'bear
("before perfect bear swims", "before the bear swims"),
# 'beard' isn't captured
("a perfect beard", "a perfect beard"),
# We handle the case where 'bear' is the end of the string
("perfect bear", "the bear"),
# 'bear' is followed by a non-space punctuation character
("perfect bear-string", "the bear-string"),
])
def test_bear_replace(string, expected):
assert bear_replace(string) == expected