Returns True if the pattern matches the given word string. The pattern can include a wildcard (*front, back*, *both*, in*side), or it can be a compiled regular expression.
(string, pattern)
| 79 | regexp = type(re.compile(r".")) |
| 80 | |
| 81 | def _match(string, pattern): |
| 82 | """ Returns True if the pattern matches the given word string. |
| 83 | The pattern can include a wildcard (*front, back*, *both*, in*side), |
| 84 | or it can be a compiled regular expression. |
| 85 | """ |
| 86 | p = pattern |
| 87 | try: |
| 88 | if p.startswith(WILDCARD) and (p.endswith(WILDCARD) and p[1:-1] in string or string.endswith(p[1:])): |
| 89 | return True |
| 90 | if p.endswith(WILDCARD) and not p.endswith("\\"+WILDCARD) and string.startswith(p[:-1]): |
| 91 | return True |
| 92 | if p == string: |
| 93 | return True |
| 94 | if WILDCARD in p[1:-1]: |
| 95 | p = p.split(WILDCARD) |
| 96 | return string.startswith(p[0]) and string.endswith(p[-1]) |
| 97 | except AttributeError: |
| 98 | # For performance, calling isinstance() last is 10% faster for plain strings. |
| 99 | if isinstance(p, regexp): |
| 100 | return p.search(string) is not None |
| 101 | return False |
| 102 | |
| 103 | #--- LIST FUNCTIONS -------------------------------------------------------------------------------- |
| 104 | # Search patterns can contain optional constraints, |