Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python multiple regular expressions

I need to apply multiple regular expressions on a string which I'm doing like this:

regex = re.compile("...")
regex2 = re.compile("...")
regex3 = re.compile("...")
regex4 = re.compile("...")
if regex.match(string) == None and regex2.match(string) == None and regex3.match(string) == None and regex4.match(string) == None:

I was wondering if there is another way to somehow merge or combine the single regular expressions or if I'm already doing it the 'right way'?

like image 913
wasp256 Avatar asked Nov 16 '25 14:11

wasp256


1 Answers

r_list = [re.compile("..."),
          re.compile("..."),
          re.compile("..."), 
          re.compile("...")]
if any(r.match(string) for r in r_list):
    # if at least one of the regex's matches do smth
like image 193
root Avatar answered Nov 19 '25 06:11

root