regex - Python re.search string search for open and close bracket [] -


can explain me why regex not getting satisfied below regex expression. let me know how overcome , check [] match.

>>> str = li= "a.b.\[c\]" >>> if re.search(li,str,re.ignorecase): ...    print("matched") ... >>> >>> str = li= r"a.b.[c]" >>> if re.search(li,str,re.ignorecase): ...    print("matched") ... >>> 

if remove open , close brackets match

>>> str = li= 'a.b.c' >>> if re.search(li,str,re.ignorecase): ...    print("matched") ... matched 

you attempting match string a.b.\\[c\\] instead of a.b.[c].
try this:

import re li= r"a\.b\.\[c\]" s = "a.b.[c]" if re.search(li, s, re.ignorecase):     print("matched") 

re.ignorecase not needed in here way.


Comments