regex - 如何在 Visual Studio 中替换部分匹配的正则表达式字符串

标签 regex visual-studio visual-studio-2008 comments replace

我想替换我的 Visual Studio 项目评论中的一个单词。为了找到这个词(让我们说它“有趣”,我可以使用正则表达式,如下例所示(仅供引用,我实际上使用空格而不是下划线,但在这里放置下划线,因为它们更容易看到)

^(:b*').*_fun_

这会找到这样的字符串

'Oh how fun it is to ride
'You are fun to talk to

但是找不到

'You are so funny!

这是正确的(这正是我想要的)。我想知道的是如何在 VS 中使用 use find/replace 将单词“fun ”替换为其他单词(例如“有趣”),这样得到的注释最终为:

'Oh how interesting it is to ride
'You are interesting to talk to
'You are so funny!

无需手动单独替换每次出现的“fun”。

如果不清楚,我只想在注释中搜索和替换(这就是正则表达式搜索的必要性)。

最佳答案

使用涵盖 _fun_ 之前所有内容的标签并在替换语句中引用它。这仍然只会替换每行出现的一个单词,但不会删除其他文本。

查找

^{:b*'.*}<fun>

替换

\1interesting

Regular Expressions in VS2008

-- 编辑 --

使用<>来标记单词的开头和结尾。这样,当“fun”出现在行尾时,正则表达式也应该匹配“fun”。

关于regex - 如何在 Visual Studio 中替换部分匹配的正则表达式字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343655/

相关文章:

c++ - 自动库选择

asp.net - 在 asp.net MVC Javascript 事件中未命中断点

c# - 如何设置项目的调试实例内存限制

JavaScript - 在 Opera 中验证十六进制?

regex - 匹配字符串之间的所有 (+) 符号

c# - 当listView中的dateTime == dateTime.Now时播放声音

visual-studio - "detach all"与 "stop debugging"

带有 Dojo/Dijit 的 Javascript 智能感知是 Visual Studio 2008

php - 使用正则表达式为 HTML 类和 ID 添加前缀?

regex - bash:从文件中读取每一行并用作正则表达式来匹配和打印列 awk