String manipulation vs Regexps

后端 未结 6 506
轮回少年
轮回少年 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:19

    Who said regexes were slow? At least in Perl they tend to be the preferred method of manipulating strings.

    Regexes are bad at some things like email validation because the subject is too complex, not because they are slow. A proper email validation regex is over 6,000 characters long, and it doesn't even handle all of the cases (you have to strip out comments first).

    At least in Perl 5, if it has a grammar it probably shouldn't be parsed with one regex.

    You should also rewrite a regex as a custom function if the regex has grown to the point it can no longer be easily maintained (see the previous email validation example) or profiling shows that the regex is the slow component of your code.

    You seem to be concerned with the speed of the regex vs the custom algorithm, but that is not a valid concern until you prove that it is with a profiler. Write the code in the most maintainable way. If a regex is clear, then use a regex. If a custom algorithm is clear, then use a custom algorithm. If you find that either is eating up a lot of time after profiling your code, then start looking for alternatives.

提交回复
热议问题