简体   繁体   English

正则表达式不会替换冒号之前的任何内容

[英]Regex doesn't replace anything before colon

I want to strip off the 're:' off subject lines in emails:我想去掉电子邮件中的're:'主题行:

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:".但是,它似乎与"re:"匹配,但与 "re:" 不匹配"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* ).仅在字符串的开头匹配( ^ )并且还删除任何后续空格( \s* )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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