jaffa
jaffa

Reputation: 27350

Regex doesn't replace anything before colon

I want to strip off the 're:' off subject lines in emails:

My string helper extension does the following:

  return Regex.Replace(value, "([re:][re :])", "",RegexOptions.IgnoreCase);

However, it seems to match on "re :", but not "re:".
Is there any reason for this and how do I go about fixing it?

Upvotes: 0

Views: 171

Answers (1)

Qtax
Qtax

Reputation: 33918

You probably mean something like:

Regex.Replace(value, "re:|re :", "", RegexOptions.IgnoreCase);

Which can also be written as:

Regex.Replace(value, "re ?:", "", RegexOptions.IgnoreCase);

And here is a possibly better expression:

Regex.Replace(value, "^\s*re\s*:\s*", "", RegexOptions.IgnoreCase);

Which only matches at the beginning of the string (^) and also removes any following spaces (\s*).

Upvotes: 2

Related Questions