Replace part of the line as a result of re.findall ()

In the next line of input, I want to replace "item" with "replace_item" based on the regex search term.

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")

      

gives the result:

 [('of', ' the ', 'item'), ('with', ' any ', 'item')]

      

I want to replace the keyword "item" in the above phrases with "replace_items".

Expected output: i have many roof item in the repeat item of the replaced_item inthe item downunder. with any replaced_item

      

+3


source to share


1 answer


You can get the expected result with a replacement string \1\2replaced_item

:

import re
pat = r"\b(see|under|in|of|with|this)\b( *.{0,4})(item)"
s = "i have many roof item in the repeat item of the item inthe item downunder. with any item"
res = re.sub(pat, r"\1\2replaced_item", s)
print(res)

      

See Python Demo



Also notice how word boundaries now limit the context for words within the interlace (as they move, only 1 word boundary is required at both ends).

Just a note: if replaced_item

is a placeholder and can start with a number, you must use r'\1\g<2>replace_item'

. \g<2>

is an unambiguous backreference entry, see python re.sub group: number after \ number SO post .

+1


source







All Articles