在 Visual Studio 中搜索和替换
在 Visual Studio 中,当我在选择范围内搜索时,我想使用正则表达式替换每行匹配的第一个实例(或第二个、第三个等)。我该怎么做?
搜索并替换
foo1 = foo1;
foo2 = foo2;
...
foo20 = foo20;
为以下内容。
foo1 = bar1;
foo2 = bar2;
...
foo20 = bar20;
In Visual Studio, when I search within a selection, I want to replace the first instance (or second, third, etc.) of a match per line using regular expressions. How would I do this?
Search and replace
foo1 = foo1;
foo2 = foo2;
...
foo20 = foo20;
into the following.
foo1 = bar1;
foo2 = bar2;
...
foo20 = bar20;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Visual Studio 2012 中,捕获组和反向引用的使用方式与 C# 中一样。您可以使用通用括号捕获它们,并使用 $0、$1 等反向引用它们。希望它有帮助!
请注意,语法
$1
用于查找替换,但\1
用于搜索字符串中的反向引用。In Visual Studio 2012, capture groups and backreferences are used just like in C#. You can capture them with common parenthesis, and backreference them with $0, $1, etc. Hope it helps!
Note that the syntax
$1
is used for find-replace, but\1
is used for backreferences in the search string.在Visual Studio 2010 及更早版本中,使用带反向引用的正则表达式
Visual Studio 的正则表达式与我学到的完全不同。我花了一些时间才找出正确答案。
搜索
Replace with
Back 引用是通过使用大括号
{foo}
进行标记
来完成的。:d+
与\d+
相同在此处了解有关 VS RegEx 的更多信息
In Visual Studio 2010 and earlier, use regular expressions with back references
Visual Studio's regular expressions are completely different from what I've learned. Took me some time to figure out the correct answer.
Search for
Replace with
Back references are done by
tagging
with curly braces{foo}
.:d+
is the same for\d+
Read more about VS RegEx here
我也可以在没有正则表达式的情况下完成:
将
= foo
替换为= bar
。如果需要正则表达式,可以使用:
替换为:
I can be done without a regular expression as well:
Replace
= foo
with= bar
.If a regular expression is needed, one could use:
Replace with:
就是这样,按照此处显示的内容键入
搜索:
(\w+\d+\s*=\s*)[^\d]+(\d+);
替换:
$1 bar$2;
了解更多:在 Visual Studio 中使用正则表达式
Here it is, type exactly as it is displayed here
Search:
(\w+\d+\s*=\s*)[^\d]+(\d+);
Replace:
$1bar$2;
Read more: Using Regular Expressions in Visual Studio
在 Visual Studio 2019 查找/替换中就像这样简单。
我需要将 FORTRAN IO 格式字符串替换为 C++ 格式,并使用子表达式和正则表达式的数量。
示例:查找:“f9.8”、“f18.3”,并替换为 %9.8f、%18.3f
reg exp: 查找 = ( f)(\d+.\d+) 替换 = %$2f
As simple as this in Visual Studio 2019 Find/Replace.
I needed to replace FORTRAN IO format string to C++ format and used sub-expression and numbers of regexp.
Example: find: "f9.8", "f18.3", in line and replace with %9.8f, %18.3f
reg exp: Find = ( f)(\d+.\d+) Replace = %$2f
如果您想要更多变量:
搜索
=
和(anynumber);
并将其替换。编辑:
该号码是可选的。
If you would be more variable:
This search for
=
and(anynumber);
and replace that between.Edit:
The number is optional.