Make your first successful regular expression match

I am trying to extract a dataset from a string that can match one of three patterns. I have a list of compiled regular expressions. I want to run them (in order) and move on to the first match.

regexes = [
    compiled_regex_1,
    compiled_regex_2,
    compiled_regex_3,
]

m = None
for reg in regexes:
    m = reg.match(name)
    if m: break

if not m:
    print 'ARGL NOTHING MATCHES THIS!!!'

This should work (not tested yet), but it's pretty ugly. Is there a better way to boil a cycle that breaks when it succeeds or explodes when it doesn't?

There may be something special for rewhich I do not know about, which allows you to test several patterns as well.

+3
source share
6 answers

else for:

for reg in regexes:
    m = reg.match(name)
    if m: break
else:
    print 'ARGL NOTHING MATCHES THIS!!!'
+6

, - , any:

if any(reg.match(name) for reg in regexes):
     ....

, .

|:

regex = re.compile(r"(regex1)|(regex2)|...")

, , , . , , , None:

>>> match = re.match("(a)|(b)|(c)|(d)", "c")
>>> match.groups()
(None, None, 'c', None)

, , - , .

, , , , .

+2

, ciruit:

m = compiled_regex_1.match(name) or
    compiled_regex_2.match(name) or
    compiled_regex_3.match(name) or
    print("ARGHHHH!")
+1

Python 2.6 :

import itertools as it

m = next(it.ifilter(None, (r.match(name) for r in regexes)), None)

ifilter genexp, , .. genexp ( "phantom for " ):

m = next((m for r in regexes for m in (r.match(name),) if m), None)

itertools , .

, 2.6, next, , . 2.5 ,

def next(itr, deft):
  try: return itr.next()
  except StopIteration: return deft
+1

- , , , .

regexps = {
  'first': r'...',
  'second': r'...',
}

compiled = re.compile('|'.join('(?P<%s>%s)' % item for item in regexps.iteritems()))
match = compiled.match(my_string)
print match.lastgroup
0

, , , , . , or . +1 Nathon OP else.

:

# alternative to any builtin that returns useful result,
# the first considered True value
def first(seq):
    for item in seq:
        if item: return item

regexes = [
    compiled_regex_1,
    compiled_regex_2,
    compiled_regex_3,
]

m = first(reg.match(name) for reg in regexes)
print(m if m else 'ARGL NOTHING MATCHES THIS!!!')
0

Source: https://habr.com/ru/post/1763232/


All Articles