简体   繁体   中英

.Net regex for string between \" "\

I have been trying to get the id from the following text

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

In C# using substring or even Regex, but nothing seems to be working. 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. Is there any way i can get the id portion from this text which is enclosed between the characters \\" "\\ ?

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

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