开发者

Regex doesn't replace anything before colon

开发者 https://www.devze.com 2023-03-19 21:42 出处:网络
I want to strip off the \'re:\' off subject lines in emails: My string helper extension does the following:

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?


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*).

0

精彩评论

暂无评论...
验证码 换一张
取 消