173

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]+\> 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?

bdhar
  • 21,619
  • 17
  • 70
  • 86

4 Answers4

259

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.

For versions Visual studio 2012 & up:
Starting with VS2012 .NET Framework regular expressions are used. So there it should be:
find include "([a-zA-Z]+\.h)",
replace with include <$1>.

Stormenet
  • 25,926
  • 9
  • 53
  • 65
Miel
  • 3,347
  • 2
  • 26
  • 39
  • 5
    This seems to differ from the standard regular expressions syntax which uses parentheses for this functionality. – Goblin Alchemist Feb 20 '12 at 17:40
  • 49
    It is now different in Visual Studio 2012. $1 should be used instead of \1. () now replaces {}. More consistant with everyone else now. http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx – oillio Apr 05 '13 at 00:31
  • 2
    Use `[a-zA-Z0-9_]` to capture more filenames. – Reinier Torenbeek Feb 10 '18 at 06:34
4

It's also possible with the short version:

Short version

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

Andreas Karz
  • 390
  • 3
  • 6
1

You need to select both Match Case and Regular Expressions for regex expressions with case. Else [a-z] won't work.enter image description here

David Morrow
  • 262
  • 4
  • 9
1

here is my use case I need to find all the html comments like this

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

this is what I used

((<!--.+?-->)|('.+?'))

I hope it can be helpful for someone

dnxit
  • 7,118
  • 2
  • 30
  • 34
  • I do not see how this answers the question at the top of this page, but it should. Please [edit] according to [answer] or delete the answer. Otherwise it risks being flagged as "not an answer" and being deleted. – Yunnosch Sep 03 '22 at 14:07