简体   繁体   中英

RegEx: Match same string into 2 named groups

I have the following C# code:

string MyString = "<a href=\"magnet:?xt=urn:btih:7f3befa467c4cac0787286c87ea264a0606066f5&dn=some.file.name.zip&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80\" title=\"Download this torrent using magnet\">...</a>[Some unwanted stuff here]<a href=[Another]>";

MatchCollection MyMatches = Regex.Matches(MyString, "(?<URL>magnet:\\?.*?)(?:\"|')");           
foreach(Match MyMatch in MyMatches)
{
    MessageBox.Show(MyMatch.Groups["URL"].Value);
}

My question is: how can I also put &dn=some.file.name.zip into a named group called <File> ?

string MyString = "<a href=\"magnet:?xt=urn:btih:7f3befa467c4cac0787286c87ea264a0606066f5&dn=some.file.name.zip&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80\" title=\"Download this torrent using magnet\">...</a>[Some unwanted stuff here]<a href=[Another]>";

MatchCollection MyMatches = Regex.Matches(MyString, "(?<URL>magnet:\\?.*(&dn=(?<File>[^&]+)).*?)(?:\"|')");           
foreach(Match MyMatch in MyMatches)
{
    MessageBox.Show(MyMatch.Groups["URL"].Value);
    MessageBox.Show(MyMatch.Groups["File"].Value);
}

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