[英]Regex doesn't replace anything before colon
我想去掉电子邮件中的're:'
主题行:
我的字符串助手扩展执行以下操作:
return Regex.Replace(value, "([re:][re :])", "",RegexOptions.IgnoreCase);
但是,它似乎与"re:"
匹配,但与 "re:" 不匹配"re:".
这有什么原因吗?我该如何修复它?
你的意思可能是这样的:
Regex.Replace(value, "re:|re :", "", RegexOptions.IgnoreCase);
也可以写成:
Regex.Replace(value, "re ?:", "", RegexOptions.IgnoreCase);
这是一个可能更好的表达方式:
Regex.Replace(value, "^\s*re\s*:\s*", "", RegexOptions.IgnoreCase);
仅在字符串的开头匹配( ^
)并且还删除任何后续空格( \s*
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.