Can someone explain me why my regex is not getting satisfied for below regex expression. Could someone let me know how to overcome and check for [] 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 I remove open and close brackets I get match
>>> str = li= 'a.b.c'
>>> if re.search(li,str,re.IGNORECASE):
... print("matched")
...
matched
You are attempting to match the string ab\\\\[c\\\\]
instead of ab[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
is not needed in here by the way.
You can try the following code:
import re
str = "a.b.[c]"
if re.search(r".*\[.*\].*", str):
print("Matched")
Output:
Matched
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.