简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM