Python: defining regex pooling

I have a list of templates like

list_patterns = [': error:', ': warning:', 'cc1plus:', 'undefine reference to']

what I want to do is create a union of all of them, giving a regular expression that matches every element in list_patterns[but doesn't seem to match any duplicate list in path_list - msw]

re.compile(list_patterns)

Is it possible?

+5
source share
6 answers

There are several ways to do this. The easiest way:

list_patterns = [': error:', ': warning:', 'cc1plus:', 'undefine reference to']
string = 'there is an : error: and a cc1plus: in this string'
print re.findall('|'.join(list_patterns), string)

Conclusion:

[': error:', 'cc1plus:']

which is great as long as the concatenation of your search patterns does not violate the regular expression (for example, if one of them contains a special regular expression character, such as an opening bracket). This can be done like this:

list_patterns = [': error:', ': warning:', 'cc1plus:', 'undefine reference to']
string = 'there is an : error: and a cc1plus: in this string'
pattern = "|".join(re.escape(p) for p in list_patterns)
print re.findall(pattern, string)

. , re.escape(), .

, , . ? , , , . , .

-, , , , .

+10
list_regexs = [re.compile(x) for x in list_patterns]
+2

, ? :

': error:|: warning:|cc1plus:|undefine reference to'?

, Python:

re.compile("|".join(list_patterns))
+1

ptrn = re.compile('|'.join(re.escape(e) for e in list_patterns))

re.escape(), - () [] |. + * .. , , escape().

, "" - ?

+1

. , , , , , , .

, , , , , , :

  • list_patterns. ( , ).
  • re.escape() .
  • (... ).
  • '|'.join() .
  • len(list_patterns).

, .

:

import re

def usedgroupindex(indexabledata):
    for i,datum in enumerate(indexabledata):
        if datum: return i
    # return None

def findallstrings(list_patterns, string):
    lp = sorted(set(list_patterns), reverse=True)
    pattern = "|".join("(%s)" % re.escape(p) for p in lp)
    # for m in re.findall(pattern, string): print (m, usedgroupindex(m))
    return ( len(set(usedgroupindex(m) for m in re.findall(pattern, string)))
             == len(lp) )

list_patterns = [': error:', ': warning:', 'cc1plus:', 'undefine reference to']
string = ' XZX '.join(list_patterns)

print ( findallstrings(list_patterns, string) )
+1

,

, , , " " " " ( | "" ).

, RE ( - ), , (), , ().

'.*?'.join(list_patterns) ( RE), , '.*?'.join(re.escape(p) for p list_patterns)).

, , , . (, itertools.permutations), '.*?' | - RE , N N! ( "N " - , N 4, 4 * 3 * 2 * 1 == 24). , , , , , .

" " ( ), , , RE - , RE relist=[re.compile(p) for p in list_patterns] " s, " all(r.search(s) for r in relist) .

, " " RE, , , , , search, ( " " ):

class relike(object):
    def __init__(self, list_patterns):
        self.relist = [re.compile(p) for p in list_patterns]
    def search(self, s):
        return all(r.search(s) for r in relist)
0
source

All Articles