Efficiently querying one string against multiple regexes

前端 未结 18 916
感情败类
感情败类 2020-12-12 17:16

Lets say that I have 10,000 regexes and one string and I want to find out if the string matches any of them and get all the matches. The trivial way to do it would be to jus

18条回答
  •  余生分开走
    2020-12-12 17:48

    If you're using real regular expressions (the ones that correspond to regular languages from formal language theory, and not some Perl-like non-regular thing), then you're in luck, because regular languages are closed under union. In most regex languages, pipe (|) is union. So you should be able to construct a string (representing the regular expression you want) as follows:

    (r1)|(r2)|(r3)|...|(r10000)
    

    where parentheses are for grouping, not matching. Anything that matches this regular expression matches at least one of your original regular expressions.

提交回复
热议问题