Efficiently querying one string against multiple regexes

前端 未结 18 891
感情败类
感情败类 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条回答
  •  Happy的楠姐
    2020-12-12 17:41

    You could combine them in groups of maybe 20.

    (?=(regex1)?)(?=(regex2)?)(?=(regex3)?)...(?=(regex20)?)
    

    As long as each regex has zero (or at least the same number of) capture groups, you can look at what what captured to see which pattern(s) matched.

    If regex1 matched, capture group 1 would have it's matched text. If not, it would be undefined/None/null/...

提交回复
热议问题