2

I'm running a little c# program where I need to extract the escape-quoted words from a string.

Sample code from linqpad:

string s = "action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult";
var pattern = "\".*?\"";
var result = Regex.Split(s, pattern);
result.Dump();

Input (actual input contains many more escaped even-number-of quotes):

"action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult"

expected result

"C:\\folder\\"

actual result (2 items)

"action = 0;
dir =  "
_____
";
result"

I get exactly the opposite of what I require. How can I make the regex ignore the starting (and ending) quote of the actual string? Why does it include them in the search? I've used the regex from similar SO questions but still don't get the intended result. I only want to filter by escape quotes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Serge P
  • 1,591
  • 8
  • 22
  • 42

2 Answers2

2

Instead of using Regex.Split, try Regex.Match.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
0

You don't need RegEx. Simply use String.Split(';') and the second array element will have the path you need. You can then Trim() it to get rid of the quotes and Remove() to get rid of the ndir part. Something like:

result = s.Split(';')[1].Trim("\r ".ToCharArray()).Remove(0, 7).Trim('"');
dotNET
  • 33,414
  • 24
  • 162
  • 251