restrict 1 word as case sensitive and other as case insensitive in python regex | (pipe)

Viewed 1509

I got the meaning of | (pipe special character) in regex, Python. It matches either 1st or 2nd.

ex : a|b Matches either a or b.

My question: What if I want to match is a with case sensitive and b with case insensitive in above example?

ex:

s = "Welcome to PuNe, Maharashtra"

result1 = re.search("punnee|MaHaRaShTrA",s)
result2 = re.search("pune|maharashtra",s)
result3 = re.search("PuNe|MaHaRaShTrA",s)
result4 = re.search("P|MaHaRaShTrA",s)

I want to search Pune in the way I have written in above statement s i.e PuNe. But I have to search Maharashtra by ignoring case. How can I search 1 word with case sensitive and other with case insensitive? So that, result1, result2, result3, result4 will give not null value.

I tried:

result1 = re.search("pune|MaHaRaShTrA",s1, re.IGNORECASE)

But this ignores the cases for both the words.

How can I restrict 1 word as case sensitive and other as case insensitive?

2 Answers
Related