Matching a list of regex to every string in a list one by one

I have a list of strings and a list of regex patterns. What I want to achieve is that for every string in the list, I want to try matching the patterns found in the regex list that I have created. As soon as a match is found, I want to no longer look in the regex list for further matches.

Following is a simple version of my code:

str = ['catihavea', 'mycatisacatbigcat', 'mycatgetangrycatacat']
pattern = ['ttt$','cats$', 'cat$','at$']
lst=[]
for string in str:
    for pat in pattern:
        match = re.search(pat, string)
        if match:
            print(match.group()) 
            lst.append(match.group())
            break
        else:
            lst.append("None")

The output I’m getting is:

[‘None’, ‘None’, ‘cat’, ‘None’, ‘None’, ‘cat’, ‘None’, ‘None’, ‘cat’]

The output I want is:

[‘None’,‘cat’,‘cat’]

I want the new list ‘lst’ and the ‘str’ to be of the same length. That is why i want to append ‘None’ if no match is found.

What is your question?

The problem with your code is your inner for loop will append a value (either a matched value or ‘None’ for each pattern checked. You don’t want that. Your code does correctly append a matched value one time and stops searching the rest of the regex expressions, but if there is no match, your else statement code runs and you end up appending a bunch of ‘None’ values when you should only append ‘None’ once all of the regex expressions do not find a match.

One approach is to declare a Boolean variable (let’s say foundMatch) and initialize it to False. Once you find a match and append it to lst, then you set the value of foundMatch to True and break (as you were already doing). Instead of an else statement, you just have an if statement after the inner for loop is complete that checks the value of foundMatch. If it is still False, then you append None to list.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.