。我使用正则表达式 \#include \"[a-z\.h]+\" 来查找包含语句。但我想知道如何构建替换正则表达式。\#include \<[a-z\.h]+\> 不起作用,也不会;它将语句 #include "......" /> 。我使用正则表达式 \#include \"[a-z\.h]+\" 来查找包含语句。但我想知道如何构建替换正则表达式。\#include \<[a-z\.h]+\> 不起作用,也不会;它将语句 #include "......"> 。我使用正则表达式 \#include \"[a-z\.h]+\" 来查找包含语句。但我想知道如何构建替换正则表达式。\#include \<[a-z\.h]+\> 不起作用,也不会;它将语句 #include "......" />
ChatGPT解决这个技术问题 Extra ChatGPT

Visual Studio、查找和替换、正则表达式

我正在尝试使用 Visual Studio 2005 中的查找和替换功能将所有 #include "whatever.h" 替换为 #include <whatever.h>。我使用正则表达式 \#include \"[a-z\.h]+\" 来查找包含语句。但我想知道如何构建替换正则表达式。

\#include \<[a-z\.h]+\> 不起作用,也不会;它将语句 #include "whatever.h" 替换为 #include <[a-z.h]+>。我应如何构建替换正则表达式以保持 whatever.h 原样?


S
Stormenet

对于之前 Visual Studio 2012 的版本:
当我这样做时它可以工作:
找到 include "{[a-zA-Z]+\.h}"
替换为 include <\1>
与您的问题最相关的部分是大括号 {} 和反向引用 \1\n 引用搜索表达式中大括号指示的第 n 个组。

适用于 Visual Studio 2012 和up:
从 VS2012 开始使用 .NET Framework 正则表达式。所以它应该是:
找到 include "([a-zA-Z]+\.h)"
替换为 include <$1>


这似乎与使用括号来实现此功能的标准正则表达式语法不同。
现在在 Visual Studio 2012 中有所不同。应该使用 $1 而不是 \1。 () 现在替换 {}。现在与其他所有人更加一致。 msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx
使用 [a-zA-Z0-9_] 捕获更多文件名。
A
Andreas Karz

短版也可以:

https://i.stack.imgur.com/1egU0.png

https://regex101.com/r/vW7Rbh/1


D
David Morrow

https://i.stack.imgur.com/GzQPV.jpg


d
dnxit

这是我的用例,我需要找到所有这样的 html 注释

<!--begin::Info-->
<!--End::Info-->

这是我用的

(()|('.+?'))

我希望它对某人有帮助