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次或多次

  • 相关阅读:
    Java算法之 二分搜寻法 ( 搜寻原则的代表)
    Java算法之 二分搜寻法 ( 搜寻原则的代表)
    基本选择器示例 改变颜色
    jquery 2.3.1 基本选择器
    ModelMapper:从对象到对象的映射库
    老司机带你在MySQL领域“大吉大利,晚上吃鸡”
    jquery getElementById
    jquery getElementsByName
    根据id获取元素
    win10能点开开始菜单但是不能点里面的功能
  • 原文地址:https://www.cnblogs.com/lovebanyi/p/264478.html
Copyright © 2011-2022 走看看