String manipulation vs Regexps

后端 未结 6 579
轮回少年
轮回少年 2021-01-18 13:45

We are often told that Regexps are slow and should be avoided whenever possible.

However, taking into account the overhead of doing some string manipulation oneself (

6条回答
  •  没有蜡笔的小新
    2021-01-18 14:12

    A nice feature of manipulating text with regular expressions is that patterns are high-level and declarative. This leaves the implementation considerable room for optimization such as factoring out the longest common prefix or using Boyer-Moore for static strings. Concise notation makes for quicker reading by experts. I understand immediately what

    if (s/^(.)//) {
      ...
    }
    

    is doing, and index($_, 0, 1) = "" looks noisy in comparison.

    Rather than the lower bound, the important consideration for regular expressions is the upper bound. It's a powerful tool, so people believe it's capable of correctly extracting tokens from XML, email addresses, or C++ programs and don't realize that an even more powerful tool such as a parser is necessary.

提交回复
热议问题