Visual Studio、查找和替换、正则表达式 [英] Visual Studio, Find and replace, regex
问题描述
我正在尝试使用 Visual Studio 2005 中的查找和替换功能将所有 #include "whatever.h"
替换为 #include <whatever.h>
.我使用正则表达式 \#include \"[az\.h]+\"
来查找包含语句.但我想知道如何构建替换正则表达式.
I am trying to replace all the #include "whatever.h"
with #include <whatever.h>
using find and replace functionality in Visual Studio 2005. I used the regex \#include \"[a-z\.h]+\"
to find the include statement. But I am wondering how frame the replace regex.
\#include \<[a-z\.h]+\>
不起作用,也不会;它将语句 #include "whatever.h"
替换为 #include <[a-z.h]+>
.我应该如何构建替换正则表达式以保留 whatever.h
原样?
\#include \<[a-z\.h]+\>
did not work and won't; it replaces the statement #include "whatever.h"
with #include <[a-z.h]+>
. How shall I frame the replace regex to retain whatever.h
as it is?
推荐答案
对于 before Visual Studio 2012 的版本:
当我这样做时它有效:
find include "{[a-zA-Z]+\.h}"
,
替换为 include <\1>
.
与您的问题最相关的部分是花括号 {}
和反向引用 \1
: \n
对第 n 组的引用由搜索表达式中的花括号表示.
For versions before Visual studio 2012:
It works when I do this:
find include "{[a-zA-Z]+\.h}"
,
replace with include <\1>
.
The most relevant parts for your question are the curly braces {}
and the back reference \1
: \n
references to the n'th group indicated by curly braces in the search expression.
对于版本 Visual Studio 2012 &向上:
从 VS2012 开始使用 .NET Framework 正则表达式.所以它应该是:
find include "([a-zA-Z]+\.h)"
,
替换为 include <$1>
.
这篇关于Visual Studio、查找和替换、正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!