Python Forum

Full Version: Doubt in Regex Lookaround
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am trying to use lookbehind for a sample code as below

import re
txt = "love regex or hate regex, can't ignore regex"
pattern = re.compile("(?<=(love|hate)\s)regex")
pattern.findall(txt)
The output of last line is ['love', 'hate']. But i am expecting the output to be['regex', 'regex'] at locations after love and hate.
Can you please check if i am missing something.
Remove the catpuring group inside the regex
pattern = re.compile("(?<=(?:love|hate)\s)regex")
Dear Gribouillis

Thanks a lot for your help. It works ok for me.
I have one more doubt. Can you plz look at it also

import re
txt = "T-STR(s)"
pat = re.compile(r"\bT-STR\(s\)\b", flags=re.IGNORECASE)
pat.findall(txt)
the output of the last line in above code is [], means an empty list,
but if i remove \b from the end, it matches to "T-STR(s)". This is strange to me as \b matches word boundaries than why issue with \b at the end.
According to the documentation, \b matches between a \w and a \W character or a \w and one end of the string. It doesn't match between a ) and the end of the string.