Skip to main content
 首页 » 编程设计

c#之在 c# 中使用后视和忽略的正则表达式替换

2024年02月02日26lovecherry

我需要从文本中删除所有“'s”,除了单词“let's”。

例如:“jerry's let's cake's”>>“jerry let's cake”。

strText = Regex.Replace(strText, @"\b(?!let's)([a-z-]+)'s\b", @"$1"); 

- 这可行,但在大文本上需要很长时间。

strText = Regex.Replace(strText, @"\b(?!let's)(?<=[a-z-]+)'s\b", ""); 
  • 这不是忽略“让我们”

我在第二个陈述中做错了什么?

请您参考如下方法:

如果这是实际上您需要的,它将是wise to not use Regex并做类似的事情:

strText = strText.Replace(" let's ", " let''s ") 
                 .Replace("'s", "") 
                 .Replace(" let'", " let's");