Merge several regexes to a single one

前端 未结 4 739
庸人自扰
庸人自扰 2021-01-12 14:15

I have several regexes (actually several thousands), and I must check if one string matches any of these regexes. It is not very efficient, so I would like to merge all thes

4条回答
  •  耶瑟儿~
    2021-01-12 15:01

    You can just concatenate the regexes using or (|) (and anchors for the beginning/end of string).

    Most good regex libraries optimize their finite state automata after they build it from your regex. PCRE does that, for instance.

    This step usually takes care of your optimization problem, ie. they apply most of the transformations you would have to do "by hand".

提交回复
热议问题