我需要用正则表达式替换句子中第一个出现的单词.
部分问题已解决,但我只需要替换完整的单词,并排除部分匹配.
例如,在"快速的棕色狐狸跳过懒狗"这句话中,我想用"猫"代替"狐狸".
我可以实现的结果如下:"快速的棕色猫咪跳过懒狗".而不是"狐狸猫".
我使用Regex.Replace方法如下:
var reg = new Regex(currentKeyword, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
reg.Replace(input, replace, 1, 0);
Run Code Online (Sandbox Code Playgroud)
var reg = new Regex(@"\b" + currentKeyword + @"\b", ...);
Run Code Online (Sandbox Code Playgroud)
这\b意味着一个单词边界.