-2

I searched ... and searched...I don't mean Can I test if a regex is valid, I want to test if a replacement string needs to be used in Regex.Replace or is a simple String.Replace going to give me the same result, only faster.

ie. for a given string replacement do I need to use

Regex.Replace(input, replacement)    

instead of

string.Replace(input, replacement)

This assumes (always a great idea) that string.Replace is faster than Regex.Replace.

edit: yes, a great idea. Not. Regex.IsMatch vs string.Contains for one.

Leaving this here as an example of premature optimization. As with most things in life - "It depends". I think that if you had hundreds or thousands of patterns then it may be worth sifting out the simple ones, depending on the likelihood of there being simple ones. For a manageable number of patterns, just do the Regex.

Community
  • 1
  • 1
CAD bloke
  • 8,578
  • 7
  • 65
  • 114
  • 4
    What is your question? – EZI Jul 06 '14 at 22:56
  • "I want to test if a replacement string needs to be used in Regex.Replace or is a simple String.Replace going to give me the same result, only faster." HOW? – CAD bloke Jul 06 '14 at 22:57
  • Are you asking to figure this our dynamically? IE, I could give you any input string and you want to determine in code, ok choose `string.Replace` and then I give you another string and you determine use `Regex.Replace` because in this instance it's faster? Or are you asking which one is faster (period)? – Prescott Jul 06 '14 at 22:58
  • Couldn't you set up some timers and test this pretty easily? – Prescott Jul 06 '14 at 22:58
  • @Prescott fair point - edited the question to clarify – CAD bloke Jul 06 '14 at 22:59
  • Would it be possible to see some sample values? – Prescott Jul 06 '14 at 23:03
  • 3
    @CADbloke *HOW?* is not a question. It implies *write if for me*. Better, you write your test cases and ask here if you get a problem. – I4V Jul 06 '14 at 23:05
  • @Prescott Basically I'm looking for the presence if a wildcard-type replacement in the string (eg. `.*` `\d` `[a-z]`) - if there's not wildcard then it's just a simple string.Replace, if there's a wildcard then it is more involved. – CAD bloke Jul 06 '14 at 23:06
  • Got it, thanks - I think you've got a reasonable answer then – Prescott Jul 06 '14 at 23:09
  • @I4V "How" is a question but your point of it not being a genius-grade question may be valid. I never claimed to be a genius. – CAD bloke Jul 06 '14 at 23:10
  • http://meta.stackoverflow.com/questions/251758/why-is-stack-overflow-so-negative-of-late – CAD bloke Jul 06 '14 at 23:13

1 Answers1

2

You can safely assume that if Regex.Escape(pattern) == pattern then you can use a good old string replacement.

But this is micro-optimization... So do measure whether this actually is worth the additional complexity. I bet the regex engine can figure that out, but I may be wrong: either way, never make assumptions out of thin air in cases like that, but back them up with measures.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158