Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Visual Studio use such a strange regex syntax

People also ask

What regex does Visual Studio use?

Visual Studio uses . NET regular expressions to find and replace text.

Why is regex so difficult?

Regular expressions are dense. This makes them hard to read, but not in proportion to the information they carry. Certainly 100 characters of regular expression syntax is harder to read than 100 consecutive characters of ordinary prose or 100 characters of C code.

What flavor of regex does VSCode use?

So, it is clear it is JS regex engine.

How do I find the regex code in Visual Studio?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.


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


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.


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.


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.