简体   繁体   中英

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 \& humanities","id":570,"prime":true,"children":[{"name":"books \& literature","id":22, ...

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

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

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM