3
在下面的輸入字符串中,我想根據正則表達式搜索條件將「item」替換爲「replaced_item」。替換re.findall()結果中的一部分字符串
re.findall(r"(\bsee\b|\bunder\b|\bin\b|\bof\b|\bwith\b|\bthis\b)(*.{0,4})(item)","i have many roof item in the repeat item of the item inthe item downunder. with any item")
給輸出:
[('of', ' the ', 'item'), ('with', ' any ', 'item')]
我想更換 「項」 關鍵字在上面的匹配短語 「replaced_items」。
Expected output: i have many roof item in the repeat item of the replaced_item inthe item downunder. with any replaced_item
1)使用原始字符串文字來定義正則表達式。另外,'findall'只會返回* captured *子匹配。 2)不清楚你的意思,請發佈失敗的代碼。 –
您需要爲您的正則表達式使用原始字符串文字。即''re.findall(r「(\ bsee \ b ...」)''否則反斜槓將被視爲控制字符 – tzaman
謝謝@WiktorStribiżew。原始字符串文字工作。我編輯了這個問題使其更加清晰。 – Apoorv