I understand how to make matching case in-sensitive in Python, and I understand how to use lookahead / lookbehinds, but how do I combine the two?
For instance, my text is
mytext = I LOVE EATING popsicles at home.
I want to extract popsicles
from this text (my target food item). This regex works great:
import re
regex = r'(?<=I\sLOVE\sEATING\s)[a-z0-9]*(?=\sat\shome)'
re.search(regex, mytext)
However, I'd like to account for the scenario where someone writes
i LOVE eating apples at HOME.
That should match. But "I LOVE eating Apples at home"
should NOT match, since Apples
is uppercase.
Thus, I'd like to have local case insensitivity in my two lookahead (?=\sat\shome)
and lookbehind (?<=I\sLOVE\sEATING\s)
groups. I know I can use re.IGNORECASE
flags for global case insensitivity, but I just want the lookahead/behind groups to be case insensitive, not my actual target expression.
Traditionally, I'd prepend (?i:I LOVE EATING)
to create a case-insensitive non-capturing group that is capable of matching both I LOVE EATING
and I love eating
. However, If I try to combine the two together:
(?i:<=I\sLOVE\sEATING\s)
I get no matches, since it now interprets the i:
as a literal expression to match. Is there a way to combine lookaheads/behinds with case sensitivity?
Edit: I don’t think this is a duplicate of the marked question. That question specifically asks about a part of a group- I’m asking for a specific subset- look ahead and behinds. The syntax is different here. The answers in that other post do not directly apply. As the answers on this post suggest, you need to apply some work arounds to achieve this functionality that don’t apply to the supposed duplicate SO post.