1

Im using C# and wanting to use the following regular expression in my code:

sDatabaseServer\s*=\s*"([^"]*)"

I have placed it in my code as:

Regex databaseServer = new Regex(@"sDatabaseServer\s*=\s*"([^"]*)"", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

I know you have to escape all parenthesis and quotes inside the string quotes but for some reason the following does still not work:

Working Version:

Regex databaseServer = new Regex(@"sDatabaseServer\s*=\s*""([^""]*)""", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

Any ideas how to get C# to see my regex as just a string? I know i know....easy question...Sorry im still somewhat of an amateur to C#...

SOLVED: Thanks guys!

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
AlpineCoder
  • 207
  • 1
  • 4
  • 13
  • Clicking a check mark beside an answer marks a question as solved; you do not need to change the title or question. – Dour High Arch Nov 09 '12 at 20:24
  • 1
    FYI, the `IgnorePatternWhitespace` option isn't doing anything for you since there's no whitespace *in* the pattern, and the `Compiled` option is probably doing more harm than good, performance-wise. ([ref](http://stackoverflow.com/a/7707369/20938)) – Alan Moore Nov 10 '12 at 05:27
  • @AlanMoore Thanks for the info. I'll remove those. – AlpineCoder Nov 15 '12 at 16:55

3 Answers3

5

You went one step too far when you escaped the parentheses. If you want them to be regex meta-characters (i.e. a capturing group), then you must not escape them. Otherwise they will match literal parentheses.

So this is probably what you are looking for:

@"sDatabaseServer\s*=\s*""([^""]*)"""
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • So ive be trying to use the above regex but for some reason VisualStudio is showing the parenthesis that comes after the second quote above as not part of the string EVEN though i escaped the double quote before it. – AlpineCoder Nov 09 '12 at 19:58
  • @ThePracticalSyde then just try this out. It is the equivalent of the regex you have given. – Martin Ender Nov 09 '12 at 19:59
  • have you tried putting that code in an editor? You can't both escape characters AND use a `@"literal"` – hometoast Nov 09 '12 at 20:01
  • 2
    @ThePracticalSyde Just as an aside why you should never use non-verbatim strings for regexes: If you want to match a literal `$` the regex requires you to write `\$`. But if you put that in a normal string, then the C# compiler will treat that as an escaped `$`, so the saved string will never contain the backslash. Hence, you need to write `\\$`, and this can get really annoying (especially since quotes will have to be escaped only once). Thus: try to use verbatim strings wherever possible (where you escape quotes by doubling them) – Martin Ender Nov 09 '12 at 20:10
  • @m.buettner Thanks for the explanation. I'm used to escaping in JavaScript and didn't realize this was an option in c#! – AlpineCoder Nov 09 '12 at 20:11
  • Nice. I thought the double quote was a VB only thing. – hometoast Nov 09 '12 at 20:47
1
string regex = "sDatabaseServer\\s*=\\s*\"([^\"]*)\""

in your first try, you forgot to escape your quotes. But since it's a string literal, escaping with a \ doesn't work.

In y our second try, you escaped the quotes, but you didn't escape the \ that's needed for your whitespace token \s

hometoast
  • 11,522
  • 5
  • 41
  • 58
  • No. Since the OP is using a verbatim string, there is no need for double escaping. The problems are the parentheses. – Martin Ender Nov 09 '12 at 20:00
0

Use \x22 instead of quotes:

string pattern = @"sDatabaseServer\s*=\s*\x22([^\x22]*)\x22";

But

Ignorepattern whitespace allows for comments in the regex pattern (the # sign) or the pattern split over multiple lines. You don't have either; remove.

A better pattern for what you seek is

string pattern =@"(?:sDatabaseServer\s*=\s*\x22)([^\x22]+)(?:\x22)";

(?: ) is match but don't capture and acts like an anchor for the parser. Also it assumes there will be at least 1 character in the quotes, so using the + instead of the *.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • This may be a matter of taste, but how does `\x22` improve readability over `""`? – Martin Ender Nov 09 '12 at 20:01
  • @m.buettner I am not necessarily looking for readability, I am looking to not have to fight escaping a " for the C# parser vs the regex parser. To that end \x22 makes it easier to work with the C# parser upfront. – ΩmegaMan Nov 09 '12 at 20:06