When to use Regex vs Built in String Methods?

后端 未结 6 668
庸人自扰
庸人自扰 2021-01-12 07:03

I\'ve noticed a lot of little debates about when to use regex and when to use a built in string function like String.Replace() (.NET).

It seems a lot of people recom

相关标签:
6条回答
  • 2021-01-12 07:23

    Built-in string replace method is faster than the regex when you want to replace substrings. Here are the benchmark numbers in Golang, I have tried to replace 3 type substrings.

    Benchmark Replace 2 --- 236 ns/op

    Benchmark Replace 5 --- 249 ns/op

    Benchmark Replace 10 --- 871 ns/op

    Benchmark Regexp 2 --- 3750 ns/op

    Benchmark Regexp 5 --- 4457 ns/op

    Benchmark Regexp 10 --- 6020 ns/op

    As you can see Replace is far better than Regexp if you are replacing known substrings. But if you have to match unknown strings or patterns then regexp might be better.

    0 讨论(0)
  • 2021-01-12 07:32

    I would tend to think that if there is a dedicated function to manipulate a string the way you want as part of the string class, it should pretty close to 'good' where-as regex is general purpose.

    But as with anything subjective, if you are concerned about performance time the different methods.

    Then again do what easiest to understand, and do performance monitoring to find the real bottle necks as you go.

    0 讨论(0)
  • 2021-01-12 07:35

    I think it's a wrong impression to use Regex as a catch-all solution when string based search/replace is possible.

    Regex is instrinsically a process of pattern matching and should be used when the types of strings you want to match are variable or only conform to a particular pattern. For cases when a simple string search would suffice, I would always recommend using the in-built methods of the String class.

    I have never seen any performance statistics suggesting that a Regex based lookup is faster or more performant than string indexing. Additionally, Regex engines vary in their execution capabilities.

    As if that were not enough, it is quite easy to construct a Regex that performs quite badly (uses a lot of backtracking, for instance) so deep knowledge of Regex is required if you really want to optimize performance using Regex matching. On the other hand, it is quite simple even for a n00b to perform string based searches or replacements.

    0 讨论(0)
  • 2021-01-12 07:41

    Regex.Replace() is much more expensive than the String.Replace() method. Use String.Replace() when possible, and use Regex when it's a necessity.

    Take a look at this benchmark to see the time differences.

    0 讨论(0)
  • 2021-01-12 07:42

    Obviously, for complex search/match/replace operations, regexes are the way to go. For simple stuff like replacing a single word by another word, normal string methods are preferred.

    But in many cases, it's not that simple. Sometimes you come across a situation where you could use standard string operations, while the regex solution is more elegant. Even if the vanilla string algorithm is 10 times faster, it's always a good idea to ask yourself if it matters in that particular piece of code (for example if the code isn't executed in a loop).

    I would prefer the readability of a simple regex operation over a more complex, but faster algorithm using pure string operations.

    Just my 2 cents...

    0 讨论(0)
  • 2021-01-12 07:45

    I just love regexes but if there is a simple xxx->replace("foo","bar") type function available it seems silly to use a power tool like regex when a simple screwdriver would do.

    If performance is an issue then regex can be very cpu consuming for simple substitutions. (Regex usually works out more efficient on a complex search/transform than a series of "simpler" calls).

    Also I get continually caught out by the "minor" implementation differences -- like Pythons implied "^...$" on the match() builtin. I was on the road with no internet access at the time and ended up buying another copy of Lutz's book to find out what was going on!

    0 讨论(0)
提交回复
热议问题