正则表达式不区分大小写搜索未匹配确切单词
我正在使用以下的正则表达式来同时搜索三种不同的字符串格式。此外,我还使用了 re.IGNORECASE
这个选项,以便可以匹配大小写不同的字符串。但是,当我进行搜索(比如 'locality')时,我却能匹配到 'localit'、'locali'、'local' 等等。我想要的只是完全匹配这个词(例如 'locality')。
另外,如果字符串的字符之间有空格(比如 'l ocal i ty'
),我希望能够忽略这些空格。我还没有找到一个 re
的方法可以做到这一点。我尝试使用 re.ASCII
,但出现了一个错误:“...ascii is invalid!” 希望能得到一些帮助。
elif searchType =='2':
print " Directory to be searched: c:\Python27 "
directory = os.path.join("c:\\","Python27")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ' '.join(str(ord(char)) for char in userstring)
regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII ))re.IGNORECASE)
for root,dirname, files in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(os.path.join(root, file))
for line in f.readlines():
#if userstring in line:
if regex.search(line):
print "file: " + os.path.join(root,file)
break
else:
#print "String NOT Found!"
break
f.close()
1 个回答
2
在正则表达式中没有这样的标志,所以你可以选择以下两种方法:
构造一个正则表达式,在每个字符后面明确匹配空白字符:
r'\s*'.join(c for c in userStrASCII)
这样做是有效的:
myre.findall(line)
可以找到 'l Oc ALi ty'或者(如果你只需要检测模式是否匹配,而不需要处理实际的匹配文本)可以使用
string.translate(,deleteChars)
来去掉行中的空白字符,然后再进行匹配。例如,在尝试匹配之前,先执行line.translate(None, ' \t\n\r').lower()
。(记得保留原始的未处理行。)