简体   繁体   English

Python re.search字符串搜索打开和关闭括号[]

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

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] . 您正在尝试匹配字符串ab\\\\[c\\\\]而不是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. re.IGNORECASE ,这里不需要re.IGNORECASE

You can try the following code: 您可以尝试以下代码:

import re
str = "a.b.[c]"
if re.search(r".*\[.*\].*", str):
   print("Matched")

Output: 输出:

Matched

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM