with open(logfile) as inf:
for line in inf:
if re.search(string,line,re.IGNORECASE):
print 'found line',line
那么如何搜索字符串包含括号, - 使用Python的字符
答案 0 :(得分:0)
import re
s = "foo[bar]baz"
m = re.search("[\[\]]", s)
print m.group(0)
# => '['
t = "foo-bar]baz"
n = re.search("[\[\]]", t)
print n.group(0)
# => ']'
事实上,re.IGNORECASE
是不必要的,因为括号没有案例。
修改强>
u = "foo\\-bar]baz"
o = re.search('[\[\]]', u) # Does this match the \ ?
print o.group(0)
# => ']'
# Behold!