簡體   English   中英

正則表達式:將相同的字符串分成2個命名組

[英]RegEx: Match same string into 2 named groups

我有以下C#代碼:

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);
}

我的問題是:如何將&dn=some.file.name.zip放入名為<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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM