Is it faster to use alternation than subsequent replacements in regular expressions

前端 未结 6 540
耶瑟儿~
耶瑟儿~ 2021-01-05 17:27

I have quite a straightforward question. Where I work I see a lot of regular expressions come by. They are used in Perl to get replace and/or get rid of some strings in text

6条回答
  •  醉话见心
    2021-01-05 17:32

    First, measure the various options on your real data, because no amount of theory will beat an experiment (if one can be done). There are many timing modules on CPAN that will help you.

    Second, if you decide to optimize the regexes, do not squish them into one giant monster by hand, try to assemble the “master” regex with code. Otherwise no-one will be able to decipher the code.

提交回复
热议问题