evfredericksen/pynacea · matching.py
python logo
def get_rule_match(rule, words, filter_list=None):
    if filter_list is None:
        filter_list = []
    filtered_positions = utilities.get_filtered_positions(words, filter_list)
    words = [word.lower() for word in words if word not in filter_list]
    regex_match = rule.compiled_regex.match(' '.join(words) + ' ')
    if regex_match is not None:
        raw_results = regex_match.group()
        group_dict = regex_match.groupdict()
        matched = replace_values(regex_match, group_dict, rule.groups)
        nums = get_numbers(regex_match, group_dict, rule.groups)
        if len(raw_results) > len(' '.join(words)):
            remaining_words = []
        else:
            remaining_words = ' '.join(words)[len(raw_results):].split()
        remaining_words = utilities.reinsert_filtered_words(
            remaining_words, filtered_positions)
        return RuleMatch(rule, matched, remaining_words, nums)
Similar code snippets
1.
RudolfCardinal/pythonlib · sql_grammar.py
Match rating: 54.42% · See similar code snippets
python logo
def sql_keyword(word: str) -> Regex:
    regex_str = word_regex_element(word)
    return make_pyparsing_regex(regex_str, caseless=True, name=word)
2.
spyder-ide/spyder · stringmatching.py
Match rating: 53.41% · See similar code snippets
python logo
def get_search_regex(query, ignore_case=True):
    """Returns a compiled regex pattern to search for query letters in order.

    Parameters
    ----------
    query : str
        String to search in another string (in order of character occurrence).
    ignore_case : True
        Optional value perform a case insensitive search (True by default).

    Returns
    -------
    pattern : SRE_Pattern

    Notes
    -----
    This function adds '.*' between the query characters and compiles the
    resulting regular expression.
    """
    regex_text = [char for char in query if char != ' ']
    regex_text = '.*'.join(regex_text)

    regex = r'({0})'.format(regex_text)

    if ignore_case:
        pattern = re.compile(regex, re.IGNORECASE)
    else:
        pattern = re.compile(regex)

    return pattern
3.
thautwarm/RBNF · _literal_matcher.py
Match rating: 52.9% · See similar code snippets
python logo
def make_regex_matcher(regex_template: str):
    regex = re.compile(regex_template)

    def match(token: Tokenizer):
        return regex.match(token.value)

    match.raw = (ConstStrPool.cast_to_const("auto_regex"), regex_template)

    return match
4.
webrecorder/pywb · regex_rewriters.py
Match rating: 52.31% · See similar code snippets
python logo
def compile_rules(rules):
        # Build regexstr, concatenating regex list
        regex_str = '|'.join(['(' + rx + ')' for rx, op, count in rules])

        # ensure it's not middle of a word, wrap in non-capture group
        regex_str = '(?<!\w)(?:' + regex_str + ')'

        return re.compile(regex_str, re.M)
5.
mediawiki-utilities/python-mwcites · doi.py
Match rating: 52.3% · See similar code snippets
python logo
def tokenize_search(text, start, lexicon=LEXICON):
    pattern = '|'.join("(?P<{0}>{1})".format(name, pattern)
                       for pattern, name in lexicon)

    group_regex = re.compile(pattern, re.I|re.U)

    match = group_regex.search(text, start)
    while match is not None:
        yield match.lastgroup, match.group(0)
        match = group_regex.search(text, match.span()[1])
6.
scrapinghub/adblockparser · parser.py
Match rating: 51.91% · See similar code snippets
python logo
def _url_matches(self, url):
        if self.regex_re is None:
            self.regex_re = re.compile(self.regex)
        return bool(self.regex_re.search(url))
7.
jeroyang/txttk · retools.py
Match rating: 51.78% · See similar code snippets
python logo
def parallel(regex_list, sort=False):
    """
    Join the given regexes using r'|'
    if the sort=True, regexes will be sorted by lenth before processing
    
    >>> parallel([r'abc', r'def'])
    'abc|def'
    >>> parallel([r'abc', r'd|ef'])
    'abc|def'
    >>> parallel([r'abc', r'(d|ef)'])
    'abc|d|ef'
    >>> parallel([r'abc', r'defg'])
    'defg|abc'
    """
    if sort:
        regex_list = sorted(regex_list, key=len, reverse=True)
    return '|'.join([unpack(regex) for regex in regex_list])
8.
theislab/scvelo · version.py
Match rating: 51.7% · See similar code snippets
python logo
def match_groups(regex, target):
    match = re.match(regex, target)
    if match is None:
        raise re.error(f"Regex does not match “{target}”. RE Pattern: {regex}", regex)
    return match.groups()
9.
pantsbuild/pants · source_file_validator.py
Match rating: 51.69% · See similar code snippets
python logo
def matches(self, s):
    """Whether the pattern matches anywhere in the string s."""
    regex_matches = self.compiled_regex.search(s) is not None
    return not regex_matches if self.inverted else regex_matches
10.
micropython/micropython · make-stmconst.py
Match rating: 51.68% · See similar code snippets
python logo
def re_match_first(regexs, line):
    for name, regex in regexs:
        match = re.match(regex, line)
        if match:
            return name, match
    return None, None