clumpter
clumpter

Reputation: 1958

C# '&' regex problem

i've got a problem with C# regular expression. There is an JSON string like this (it's from Google Insights page):

{"name":"all categories","id":0,"prime":true,"children":[{"name":"arts \u0026 humanities","id":570,"prime":true,"children":[{"name":"books \u0026 literature","id":22, ...

Now I want to write a regex to find, for example, books \u0026 literature -- but I can't. Neither Regex.Match(html, "books & literature", RegexOptions.IgnoreCase) nor Regex.Match(html, "books \\u0026 literature", RegexOptions.IgnoreCase) doesn't works. What am I doing wrong?

Upvotes: 0

Views: 174

Answers (1)

Gabe
Gabe

Reputation: 86708

Since the string you're searching for has a literal \, you need to have a literal backslash escaped in your regex, either by @"books \\u0026 literature" or "books \\\\u0026 literature".

For example:

Regex.Match(html, @"books \\u0026 literature", RegexOptions.IgnoreCase)

Upvotes: 5

Related Questions