0

I'm trying to extract some digits from a string: foo=bar&hash=00000690821388874159\";\n

I tried making a group for the digit, but it always return an empty string.

string matchString = Regex.Match(textBox1.Text, @"hash=(\d+)\\").Groups[1].Value;

I never use regex, so please tell me what I'm missing here.

Johan
  • 35,120
  • 54
  • 178
  • 293
  • It **works** as expected. `string matchString = Regex.Match(@"foo=bar&hash=00000690821388874159\"";\n", @"hash=(\d+)\\").Groups[1].Value;` returns `00000690821388874159` – L.B Jan 04 '14 at 21:30
  • 1
    @L.B. that's because you used the `@` which read the \ as a normal character and not an escape sequence. – dee-see Jan 04 '14 at 21:35

1 Answers1

6

There is no \\ in your string, the \ is in fact used to escape a quote so that's why the regex doesn't match. This works:

string matchString = Regex.Match(textBox1.Text, @"hash=(\d+)""").Groups[1].Value;

http://dotnetfiddle.net/2U0lkI

dee-see
  • 23,668
  • 5
  • 58
  • 91
  • 2
    +1. Side note: original string looks like query string and there are much better way to parse it `HttpUtility.ParseQueryString` (if reference is acceptable) See also [How to parse a query](http://stackoverflow.com/questions/68624/how-to-parse-a-query-string-into-a-namevaluecollection-in-net) – Alexei Levenkov Jan 04 '14 at 21:38