class Pattern(object):

    def __init__(self, text='', escape=None):
        self.text = text
self.matchers = []
escape = not self.text.startswith('!') if escape is None else False
if escape:
    self.regex = re.sub('([\\[\\].?+\\-()\\^\\\\])', '\\\\\\1', self.text)
else:
    self.regex = self.text[1:]
self.size = len(re.findall('(\\$[A-Za-z0-9\\-_]+)', self.regex))
self.regex = re.sub('(\\$[A-Za-z0-9\\-_]+)', Replacer(), self.regex)
self.regex = re.sub('\\s+', '\\\\s+', self.regex)

def search(self, text):
    return re.search(self.regex, text)

def findall(self, text, max_depth=1.0):
    results = []
length = float(len(text))
for result in re.finditer(self.regex, text):
    if result.start() / length < max_depth:
        results.extend(result.groups())
return results

def match(self, text):
    result = map(lambda x: (x.groupdict(), x.start()), re.finditer(self.
        regex, text))
if result:
    return result
else:
    return []
Processing malformed text data with machine learning or NLP