简体   繁体   中英

C# innerhtml regex

Okay so I'm trying to extract particular data from innerhtml,

example of html -

<list class="bla">
    <img src="http://www.example.com?id=1&number=1" src2="http://www.example.com?id=1&number=1">
</list>

I'm currently using

foreach (Match m in Regex.Matches(innerHtml, @"id=(?<id>\d+)&amp;count=(?<count>\d+)"))

but this will get both ids&count from both srcs, how can i just target

src2=

for

id=&count=

instead of getting both

First, return your target attribute value with selenium, an example can be found how to get an attribute value from a href link in selenium

Once you have done that use the following code:

string src2 = "http://www.example.com?id=1&number=1";
        Regex regex = new Regex("([0-9])");
        Match match = regex.Match(src2);
        if (match.Success)
        {
            Console.WriteLine(match.Groups[1]);
        }
        Console.ReadKey();

Basically, group 1 is where the first occurrence (id) of numeric value group 2 is the second occurrence of numeric (number) value.

More helpful links: https://www.dotnetperls.com/regex-groups https://regex101.com/

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