zoukankan      html  css  js  c++  java
  • 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(
    07).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次或多次

  • 相关阅读:
    全球视角商讨Linux的将来生长三趋势
    Firefox 3.0新版应战IE欣赏器 年夜战在即
    讲解SQL与Oracle外键束厄狭窄中的级联删除
    在Oracle中添加用户 赋权 修正暗码 解锁
    一个完好的Oracle rman备份规复参考示例
    Novell即将面临FSF起诉 终了其发行Linux
    Linux之x登录前后的转变
    Ubuntu Linux 8.04 Vsftp 虚构用户设置
    mysql修复坏表的步履办法
    用UTL_INADDR包获取曾经衔接用户的IP地址
  • 原文地址:https://www.cnblogs.com/lovebanyi/p/264478.html
Copyright © 2011-2022 走看看