-2

I have a regex string:

string regex =
                "\"\\d*\",\"(?<url>\\w|\\d|[().,-–_'])\".*";

And a string I want to match it against:

string line =
   "\"4\",\"1800_in_sports\",\"24987709\",\"\",\"1906\",\"20171028152258\"";

When I try to get the url category, or even check for a match, there is no match:

var result = Regex.Match(line, regex);
string output = result.Groups["url"].Value;

If i try Regex.IsMatch(..) it also returns false. I used http://regexstorm.net/tester to test this and it works there, but, not when I run the code.

In RegexStorm I used the pattern:

"\d{1,3}","(?<url>\w|\d|\n|[().,-–_'])+?"
rw_
  • 81
  • 11
  • 1
    Please use [verbatim strings](https://stackoverflow.com/questions/3311988/) and tell us the expression you used in RegexStorm.. – Dour High Arch Oct 28 '17 at 17:17
  • Okay I didn't realize you could use double quotes in a verbatim string, I will try that. Thanks. I also edited the question to include the pattern I used in RegexStorm. – rw_ Oct 28 '17 at 17:24

2 Answers2

0

Replace \\d with just \d and \\w with just \w.

Philip Smith
  • 2,741
  • 25
  • 32
  • This will not compile due to "unrecognized escape sequence". If I add $ to the beginning of the string I cannot add \". – rw_ Oct 28 '17 at 17:13
  • \d says match a digit, \\d says match "\d", You want the former not the latter. – Philip Smith Oct 28 '17 at 17:15
  • When the string is read with Regex it sees "\d" not "\\d". For example, string regex = "\"\\d*\""; would work. – rw_ Oct 28 '17 at 17:19
0

As Dour High Arch mentioned, verbatim string should be used. Adding double quotes in front of double quotes allows for verbatim strings.

Changing string regex to:

string regex =
                @"""\d{1,3}"",""(?<url>\w|\d|\n|[().,-–_''])+?""";

Now returns a match.

rw_
  • 81
  • 11