and has 1 comment

As with all the programmer questions, I will update the post with the answer after people comment on this. Today's question is:
You have a list of regular expressions for strings to be matched against. You need to turn them into a single regular expression. How can you do it so that a string needs to match any of the initial regular expressions? How can you do it to match them all at the same time?

Comments

Cristian Lupașcu

For matching any pattern, just concatenate them with pipe symbols: (regex1)|(regex2)|(regex3) Matching all of them can be achieved using positive look-aheads: (?=.*regex1)(?=.*regex2)regex3 Note that the `.*` might become expensive. If the patterns attempt to match the entire string (are of the form `^...$`), then the `.*`s can be left out.

Cristian Lupașcu

Post a comment