开发者

C# '&' regex problem

开发者 https://www.devze.com 2023-03-19 08:40 出处:网络
i\'ve got a problem with C# regular expression. There is an JSON string like this (it\'s from Google Insights page):

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 l开发者_开发知识库iterature", 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 \\u0026 literature" or "books \\\\u0026 literature".

For example:

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

精彩评论

暂无评论...
验证码 换一张
取 消