ASP. NET access to Web document is often used
Edited by: Cao Yongxi-Blog Park
1. Get a tag within a class's div
Get tags in <div class= "ImgList2" >****</div>
Method One:
string g = " <div.*?class=\ "Imglist2\" > (? = new Regex (g, Regexoptions.none); MatchCollection MC = Reg. Matches (strresult); string v = "" ; foreach (Match m in MC) {v + = m.value + \r\n " ; }
View Code
Method Two (common method, get the content between the specified before and after content):
string " <div class=\ "imglist2\" > " " </div> ");
Public Static stringGetValue (stringStrstringStartstringend) {Regex regex=NewRegex (string. Concat (New string[] { "(? <= (", start,") ) [. \\s\\s]*? (?=(", End,"))"}), Regexoptions.multiline|regexoptions.singleline); returnregex. Match (str). Value; }
View Code
2. Get all the A-label href and text
Get <div class= "page both\" ></div> all the A tags in the href and text
stringList_page_group_str = GetValue (Strresult.trim (),"<div class=\ "page both\" >","</div>"); Regex Reg=NewRegex (@"(? is) <a (?).:(?! href=).) *href= ([' ""]?) (? <url>[^ "" \s>]*) \1[^>]*> (?<text> (?:(?! </?a\b).) *) </a>"); MatchCollection MC=Reg. Matches (LIST_PAGE_GROUP_STR); foreach(Match minchMC) {stringURL = m.groups["URL"]. Value +"\ n"; stringText = m.groups["text"]. Value +"\ n"; }
View Code
An example of an ASP. NET Regular Expression learning