tanascius
tanascius

Reputation: 53944

Why does Visual Studio use such a strange regex syntax

Is there any reason why Visual Studio uses such a strange syntax (for instance in the search/replace dialog)?

Instead of writing \s*(\w+) = new Process\(\) I have to write :b*{:a+} = new Process\(\).
I am always struggling with this syntax - especially since the normal .NET syntax is the former one.

This is an incomplete comparison between the two syntaxes:

What            Visual  .NET   Comment
                Studio
----------------------------------------------------------------
Tab/Spaces      :b       \s    Either tab or space
Alphanumeric    :a       \w    ([a-zA-Z0-9])
Subexpression   {}       ()
Substitution    \n       $n    Substitutes the substring matched 
                               by a numbered subexpression.
Backreference   \n       \n    Matches the value of a numbered 
                               subexpression.
----------------------------------------------------------------

See here (Visual Studio, C#) for more information.

Is there any reason for this? Is it historical? Is there any advantage?

Upvotes: 30

Views: 7012

Answers (4)

stema
stema

Reputation: 92986

The good news:

Since Visual Studio 2013, the search uses standard .NET regular expressions:

Visual Studio uses .NET Framework regular expressions to find and replace text. For more information about .NET regular expressions, see .NET Framework Regular Expressions. Before Visual Studio 2012, Visual Studio used custom regular expression syntax in the Find and Replace windows. See Visual Studio Regular Expression Conversions for an explanation of how to convert some of the more commonly-used custom regular expression symbols to the .NET versions.

No need anymore to learn a strange regular expression flavour.

Upvotes: 18

bobobobo
bobobobo

Reputation: 67244

Well, they tried to make it "easier" to use, it seems, for a C++ coder.

Consider they provided new non-standard expressions, like

C/C++ Identifier
:i
Matches the expression ([a-zA-Z_$][a-zA-Z0-9_$]*)

Quoted string
:q
Matches the expression (("[^"]*")|('[^']*')).

I think the reason it has completely different syntax is so the regex user clearly sees its non-standard and not the same as normal regex.

Which is good. All in all its a good regex engine and easy to use.

Upvotes: 6

Ted
Ted

Reputation: 7251

I quote Coding Horror:

However, you're in for an unpleasant surprise when you attempt to actually use regular expressions to find anything in Visual Studio. Apparently the Visual Studio IDE has its own bastardized regular expression syntax. Why? Who knows. Probably for arcane backwards compatibility reasons, although I have no idea why you'd want to perpetually carry forward insanity. Evidently it makes people billionaires, so who am I to judge.

http://www.codinghorror.com/blog/2006/07/the-visual-studio-ide-and-regular-expressions.html

Upvotes: 25

Michael Dorgan
Michael Dorgan

Reputation: 12515

My best and most honest answer is because they are Microsoft therefore they can use whatever standard they choose. Some programmer somewhere probably decided that the above syntax was clearer to them or easier to code for and therefore it became canon law.

Upvotes: 1

Related Questions