python正则匹配目录、文件名

时间:2021-10-16 19:10:50
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# vim:set shiftwidth=2 tabstop=2 expandtab textwidth=79:

import re

filename = "/Users/frankyu/Develop/sso/socialbase/login/dcorators.py"
inputstr = 'Uup'

inputstr = re.escape(inputstr)
m = re.search('.*' + inputstr + '.*', filename) # 普通匹配
if not m:
pattern = '.*'
for i in range(len(inputstr)):
pattern += re.escape(inputstr[i]) + '.*' # 单个字符匹配
m = re.search(pattern, filename)
if not m:
inputstr = inputstr.replace(' ', '') # 去空格后,单个字符匹配
pattern = '.*'
for i in range(len(inputstr)):
pattern += inputstr[i] + '.*'
m = re.search(pattern, filename)
if not m:
print 'can not find it'
else:
print m.group(0)
else:
print m.group(0)
else:
print m.group(0)