The Wanderer
The Wanderer

Reputation: 3271

Python case insensitive find and replace with the same found word

I know this question has already been answered before here Case insensitive replace but mine is a little different.

What I want is to search for certain keywords in a text and replace by surrounding them with <b> and </b>. And there are four different possibilities explained through an example below:

Keywords = ['hell', 'world']

Input Sentence = 'Hell is a wonderful place to say hello and sell shells'

Expected Output 1 = '<b>Hell</b> is a wonderful place to say hello and sell shells' -- (not replaced by keyword 'hell' but the found word 'Hell'. Only complete matches replaced.)

Expected Output 2 = '<b>Hell</b> is a wonderful place to say <b>hello</b> and sell shells' -- (only the matching words beginning with the keyword are replaced. Note that the whole word is getting replaced even if the match is partial)

Expected Output 3 = '<b>Hell</b> is a wonderful place to say <b>hello</b> and sell <b>shells</b>' -- (Any occurrence of hell is replaced but by the complete matching word)

Expected Output 4 = '<b>Hell</b> is a wonderful place to say <b>hell</b>o and sell s<b>hell</b>s' -- (Any occurrence of hell is replaced but NOT by the complete matching word. The casing of the matching word is left intact)

The linked SO question, replaces the word by the found keyword which is not what I want. I want to keep the casing of the input sentence intact. Can someone please help me find solution to all the above four cases?

The code that I have tried:

import re
insensitive_hippo = re.compile(re.escape('hell'), re.IGNORECASE)
insensitive_hippo.sub('hell', 'Hell is a wonderful place to say hello and sell shells')
'hell is a wonderful place to say hello and sell shells'

But this doesn't keep the found word intact.

Upvotes: 2

Views: 1429

Answers (1)

vks
vks

Reputation: 67968

print re.sub(r"\b(hell)\b",r"<b>\1</b>",x,flags=re.I)

print re.sub(r"\b(hell\S*)",r"<b>\1</b>",x,flags=re.I)

print re.sub(r"\b(\S*hell\S*)",r"<b>\1</b>",x,flags=re.I)

print re.sub(r"(hell)",r"<b>\1</b>",x,flags=re.I)

Output:

<b>Hell</b> is a wonderful place to say hello and sell shells
<b>Hell</b> is a wonderful place to say <b>hello</b> and sell shells
<b>Hell</b> is a wonderful place to say <b>hello</b> and sell <b>shells</b>
<b>Hell</b> is a wonderful place to say <b>hell</b>o and sell s<b>hell</b>s

Upvotes: 2

Related Questions