href 一个正则表达式的解析 ? 号解析
生活随笔
收集整理的這篇文章主要介紹了
href 一个正则表达式的解析 ? 号解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
ArrayList?linkLocal????=?new?ArrayList();?ArrayList?linkExternal?=?new?ArrayList();?
//?Dodgy?Regex?will?find?*some*?links?
foreach?(Match?match?in?Regex.Matches(htmlData?
????,?@"(?<=<(a|area)\s+href="").*?(?=""\s*/?>)"?
????,?RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture))?{?
????//?Regex?matches?from?opening?"quote
????link?=?match.Value;
????//?find?first?space?(ie?no?spaces?in?Url)
????int?spacePos?=?link.IndexOf('?');
????//?or?first?closing?quote?(NO?single?quotes)?
????int?quotePos?=?link.IndexOf('"');
????int?chopPos?=?(quotePos<spacePos?quotePos:spacePos);
????if?(chopPos?>?0)?{
????//?chopPos?if?quote?or?space?first?the?at?URL?end
????????link?=?link.Substring(0,chopPos);
????}?
????if?(?(link.Length?>?8)?&&?
?????????(link.Substring(0,?7).ToLower()?==?"http://")?)?{
????????//?Assumes?all?links?beginning?with?http://?are?_external_?
????????linkExternal.Add(link)?;?
????}?else?{?
????????//?otherwise?they're?"relative"/internal?links
????????//?so?we?concatenate?the?base?URL?
????????link?=?startingUrl?+?link;?
????????linkLocal.Add(link);?
????}?
}
?
.*? 非貪婪或最小匹配.
?<= 正向引用不包含在匹配值里
?= 同上.. (不過上面的哪個沒有了<號就會造成不同的結果了)
"" 因為前面加了個@ 所以這邊的""變成了"的意思.
(a|area) 其它的任意一個
RegexOptions.ExplicitCapture 指得沒有命名的不能捕獲..其它?<=和?=以經代替了它的作用了
另一種方法的捕獄.括號加命名 (?<banyi>.*?)到時候就可以match.Groups["banyi"].Value這種形式來獲得了 Replace的時候也可以指定的
?的另一個作用就是 匹配0次或一次了 +號是一次或多次*號是0次或多次
轉載于:https://www.cnblogs.com/lovebanyi/archive/2005/10/29/264478.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的href 一个正则表达式的解析 ? 号解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话设计模式——目录
- 下一篇: 怎么还不见2005的中文版出来的?