简体   繁体   English

.Net 正则表达式用于 \\" "\\ 之间的字符串

[英].Net regex for string between \" "\

I have been trying to get the id from the following text我一直在尝试从以下文本中获取id

<body id=\"body\" runat=\"server\"> 

In C# using substring or even Regex, but nothing seems to be working.在 C# 中使用子字符串甚至 Regex,但似乎没有任何效果。 No matter what regex i use, i always get the whole line back.无论我使用什么正则表达式,我总是能得到整行。 I have been trying to use ^id , ^id.* , ^id=\\\\\\\\\\\\\\\\.* and id=.* but they don't either work or give me the desired output.我一直在尝试使用^id^id.*^id=\\\\\\\\\\\\\\\\.*id=.*但它们既不工作也不给我所需的输出。 Is there any way i can get the id portion from this text which is enclosed between the characters \\" "\\ ?有什么办法可以从包含在字符\\" "\\之间的文本中获取 id 部分?

Try this:尝试这个:

        string htmlString = "<body id=\"body\" runat=\"server\">";

        Regex regex = new Regex("id=\"(.*?)\"");
        Match m = regex.Match(htmlString);
        Group g = m.Groups[1];
        string id = g.ToString();

        Console.WriteLine(id); //body

Test here: http://rextester.com/BQSF93427在这里测试: http : //rextester.com/BQSF93427

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

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