Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912434
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2010-02-09 10:46:06

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有一定的类似性。
阅读(20687) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~