import re
from normalizing import normalize
_match_pattern_tokenizer = re.compile('(\*|\?)')
def eq(str1, str2, ignore=[], caseless=True, spaceless=True):
str1 = normalize(str1, ignore, caseless, spaceless)
str2 = normalize(str2, ignore, caseless, spaceless)
return str1 == str2
def eq_any(str_, str_list, ignore=[], caseless=True, spaceless=True):
str_ = normalize(str_, ignore, caseless, spaceless)
for s in str_list:
if str_ == normalize(s, ignore, caseless, spaceless):
return True
return False
def any_eq_any(strs1, strs2, ignore=[], caseless=True, spaceless=True):
for s in strs1:
if eq_any(s, strs2, ignore, caseless, spaceless):
return True
return False
def matches(string, pattern, ignore=[], caseless=True, spaceless=True):
string = normalize(string, ignore, caseless, spaceless)
pattern = normalize(pattern, ignore, caseless, spaceless)
regexp = _get_match_regexp(pattern)
return re.match(regexp, string, re.DOTALL) is not None
def _get_match_regexp(pattern):
regexp = []
for token in _match_pattern_tokenizer.split(pattern):
if token == '*':
regexp.append('.*')
elif token == '?':
regexp.append('.')
else:
regexp.append(re.escape(token))
return '^%s$' % ''.join(regexp)
def matches_any(string, patterns, ignore=[], caseless=True, spaceless=True):
for pattern in patterns:
if matches(string, pattern, ignore, caseless, spaceless):
return True
return False
def any_matches(strings, pattern, ignore=[], caseless=True, spaceless=True):
for string in strings:
if matches(string, pattern, ignore, caseless, spaceless):
return True
return False
def contains(str1, str2, ignore=[], caseless=True, spaceless=True):
str1 = normalize(str1, ignore, caseless, spaceless)
str2 = normalize(str2, ignore, caseless, spaceless)
return str2 in str1
def contains_any(string, strings, ignore=[], caseless=True, spaceless=True):
for s in strings:
if contains(string, s, ignore, caseless, spaceless):
return True
return False
文件路径:C:\Python26\lib\site-packages\robot\match.py
功能:自定义的常见的正则表达式的匹配,以后需深入了解。与C:\Python26\lib\site-packages\robot\normalizing.py有一定的类似性。
阅读(20681) | 评论(0) | 转发(0) |