Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression replace in Visual Studio

Using Visual Studio 2010 I would like to do a project level regular expression replace as below.

Find: #region {any string here}

Replace: #region - string from above -

I tried the below:

region\s'{[^]+}'

region '{[^]+}'

region {:q}

But the IDE complains about an incorrect pattern. How can I fix this?

like image 525
Deeptechtons Avatar asked Apr 23 '12 12:04

Deeptechtons


People also ask

How do I change the regex code in Visual Studio?

How to enable VSCode regex replace. First, you need to press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to open up search and replace tool. In order to activate regex search and replace in VSCode, you have to click on the . * button near the input.

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.

How do I find and replace in Visual Studio?

You can find and replace text in the Visual Studio editor by using Find and Replace (Ctrl+F or Ctrl+H) or Find/Replace in Files (Ctrl+Shift+F or Ctrl+Shift+H). You can also find and replace only some instances of a pattern by using multi-caret selection.

What is regex replace in C#?

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. public: static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::String ^ replacement); C# Copy.


2 Answers

Ahhh, Visual Studio regexes... They shouldn't be called a regex since they diverge to much of what is "standard"

I fired up VS and after some trial and error this works:

search:

\#region \{{.*}\}

replace:

#region - \1 -
like image 167
buckley Avatar answered Oct 25 '22 19:10

buckley


Try:

Search: {\#region:b+}{.*}

Replace: \1 - \2 -

If you're specifically searching for the '{' and '}',

Search: {\#region:b+}\{{.*}\}

With quotes:

Search: {\#region:b+}{'.*'}

To remove quotes:

Search: {\#region:b+}'{.*}'

like image 32
DaveyBoy Avatar answered Oct 25 '22 17:10

DaveyBoy