When to use Regex vs Built in String Methods?

后端 未结 6 699
庸人自扰
庸人自扰 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: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!

提交回复
热议问题