Search code examples
c#regexvisual-studio-2019string-literalsquotation-marks

C# Regular expressions, string variables, and inevitable quotation marks


Although this is a basic example, there are so many questions around escaping quote marks that this basic question about string variables seems to get lost in the 'noise'.

For purposes of this question, C# is always in the context of Visual Studio C#, in this case Visual Studio-2019.

In C#, both the variable in the string that I want to test for a pattern match and the string that contains the pattern are surrounded by quote marks. These quote marks are present in the C# program-code and the debugger string variable values as well. This seems to be inevitable.

Since these quote marks are part of the C# string variables themselves, I would hope that they would just be ignored by regex as part of the standard syntax.

This appears to be the case. However, I want to verify that this works correctly, and how it works.

Example:

string ourTestString = "Smith";
string ourRegexToMatch = "^(Sm)";
Regex ourRegexVar = new Regex(ourRegexToMatch, RegexOptions.Singleline);
var matchColleciton = ourRegexVar.Matches(ourTestString);
bool ourMatch = matchColleciton.Count == 1;

The intent is to match for Sm at the beginning of the line and it is currently case sensitive.

In the above code, ourMatch is indeed true, as expected/hoped for.

It appears in the debugger that the ourRegexVar itself does not have the quote marks that surround the C# variable. There are curly brackets around everything which I would suppose is standard for such Regex variables.

One could easily imagine complex scenarios that involve strings that really do have quotation marks and escaped quotation marks and so forth, so it could get much more complicated than the above rather simple example.

My question is: For purposes of regex and C# variables is it ALWAYS the case that both for the ourTestString C# string variable and the ourRegexToMatch C# variable, it is exactly like the compiler-induced "" for a C# string variable are not there?


Solution

  • The quotes tell the compiler that it's a string. Strings in memory aren't delimited by quotes.

    If you do have a quote in a string, like Sm"ith, you have to escape it, like so:

    var s1 = "Sm\"ith";
    var s2 = @"Sm""ith";