简体   繁体   English

C#'&'正则表达式问题

[英]C# '&' regex problem

i've got a problem with C# regular expression. 我对C#正则表达式有疑问。 There is an JSON string like this (it's from Google Insights page): 有一个这样的JSON字符串(来自Google Insights页面):

{"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. 现在,我想编写一个正则表达式来查找books \& literature ,例如books \& literature -但我找不到。 Neither Regex.Match(html, "books & literature", RegexOptions.IgnoreCase) nor Regex.Match(html, "books \\\& literature", RegexOptions.IgnoreCase) doesn't works. Regex.Match(html, "books & literature", RegexOptions.IgnoreCase)Regex.Match(html, "books \\\& literature", RegexOptions.IgnoreCase)无效。 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" . 由于您要搜索的字符串具有文字\\ ,因此您需要在正则表达式中转义文字反斜杠,可以使用@"books \\\& literature""books \\\\\\\& literature"转义。

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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