dan-O
dan-O

Reputation: 354

Visual Studio Regex: Replace all #include statements using backslash with forward slash

In my C++ code, I have a mixed use of backslash and forward slash for my #include statements. I would like to standardize on using forward slash.

Keep in mind that the includes could look like any of the following:

#include "file.h"
#include <file.h>
#include "dir\file.h"
#include <dir\file.h>
#include <dir1\dir2\file.h>
#include "..\file.h"

etc.

Does anyone know of a good way to use Visual Studio 2013's "Find and Replace" regex functionality to do this?

Upvotes: 4

Views: 1087

Answers (1)

Andris Leduskrasts
Andris Leduskrasts

Reputation: 1230

Find: (#include.*?)\\

Replace with: $1/

The problem is that when you have a requirement (having #include.* in your case) but multiple matches (as there can be any number of backslashes in the filepath), it's not easy (and sometimes impossible) to write a general-purpose solution.

However, to keep it simple, you can use the mentioned regex and replace to find the first backslash in all the lines, replace it with forward slash. Then you can repeat it as many times as you need until you have no matches found (the amount of times is equal to the highest depth of filepath).

Upvotes: 6

Related Questions