html
  • regex
  • 2016-04-22 9 views 1 likes 
    1

    htmlの<a>タグをスクラップする必要があります。スクラップアンカー(<a>)htmlタグ

    私の目標は、href属性内に有効なリンクを持つタグをスクラップすることです。

    私は私が答えに非常に近いと思うが、これは私が書いた正規表現です:

    <a .*href=("|').*\.asp("|').*?>.*?<\/a> 
    

    http://regexr.com/3d989

    FIRST ISSUE:

    結果:

    <a id='topnavbtn_tutorials' href='javascript:void(0);' onclick='w3_open_nav("tutorials")' title='Tutorials'>TUTORIALS <i class='fa fa-caret-down'></i><i class='fa fa-caret-up' style='display:none'></i></a><a id='topnavbtn_references' href='javascript:void(0);' onclick='w3_open_nav("references")' title='References'>REFERENCES <i class='fa fa-caret-down'></i><i class='fa fa-caret-up' style='display:none'></i></a><a id='topnavbtn_examples' href='javascript:void(0);' onclick='w3_open_nav("examples")' title='Examples'>EXAMPLES <i class='fa fa-caret-down'></i><i class='fa fa-caret-up' style='display:none'></i></a><a href='/forum/default.asp'>FORUM</a> 
    

    と私だけが必要です:

    <a href='/forum/default.asp'>FORUM</a> 
    

    SECOND ISSUE:

    結果:

    <a href='/html/default.asp' class='w3-hide-small' title='HTML Tutorial'>HTML</a><a href='/css/default.asp' class='w3-hide-small' title='CSS Tutorial'>CSS</a><a href='/js/default.asp' class='w3-hide-small' title='JavaScript Tutorial'>JAVASCRIPT</a><a href='/sql/default.asp' class='w3-hide-small' title='SQL Tutorial'>SQL</a><a href='/php/default.asp' class='w3-hide-small' title='PHP Tutorial'>PHP</a><a href='/bootstrap/default.asp' class='w3-hide-small' title='Bootstrap Tutorial'>BOOTSTRAP</a><a href='/jquery/default.asp' class='w3-hide-small' title='jQuery Tutorial'>JQUERY</a><a href='/angular/default.asp' class='w3-hide-small' title='Angular Tutorial'>ANGULAR</a><a href='/xml/default.asp' class='w3-hide-small' title='XML Tutorial'>XML</a> 
    

    と私は別々の結果として、それらを必要とする:

    <a href='/html/default.asp' class='w3-hide-small' title='HTML Tutorial'>HTML</a> 
    
    <a href='/css/default.asp' class='w3-hide-small' title='CSS Tutorial'>CSS</a> 
    
    <a href='/js/default.asp' class='w3-hide-small' title='JavaScript Tutorial'>JAVASCRIPT</a> 
    

    のように...

    +0

    は、 "これは私が書いた正規表現である"​​ - それはリンクです。あなたのコードを質問に入れてください。 – Quentin

    答えて

    -1
    $string = "<a href='/html/default.asp' class='w3-hide-small' title='HTML Tutorial'>HTML</a><a href='/css/default.asp' class='w3-hide-small' title='CSS Tutorial'>CSS</a><a href='/js/default.asp' class='w3-hide-small' title='JavaScript Tutorial'>JAVASCRIPT</a><a href='/sql/default.asp' class='w3-hide-small' title='SQL Tutorial'>SQL</a><a href='/php/default.asp' class='w3-hide-small' title='PHP Tutorial'>PHP</a><a href='/bootstrap/default.asp' class='w3-hide-small' title='Bootstrap Tutorial'>BOOTSTRAP</a><a href='/jquery/default.asp' class='w3-hide-small' title='jQuery Tutorial'>JQUERY</a><a href='/angular/default.asp' class='w3-hide-small' title='Angular Tutorial'>ANGULAR</a><a href='/xml/default.asp' class='w3-hide-small' title='XML Tutorial'>XML</a>"; 
    
    preg_match_all('%<a href=\'/.*?\'>.*?</a>%s', $string, $matches, PREG_PATTERN_ORDER); 
    for ($i = 0; $i < count($matches[0]); $i++) { 
        echo $matches[0][$i]; 
    } 
    

    OUTPUT:

    <a href='/html/default.asp' class='w3-hide-small' title='HTML Tutorial'>HTML</a> 
    <a href='/css/default.asp' class='w3-hide-small' title='CSS Tutorial'>CSS</a> 
    <a href='/js/default.asp' class='w3-hide-small' title='JavaScript Tutorial'>JAVASCRIPT</a> 
    <a href='/sql/default.asp' class='w3-hide-small' title='SQL Tutorial'>SQL</a> 
    <a href='/php/default.asp' class='w3-hide-small' title='PHP Tutorial'>PHP</a> 
    <a href='/bootstrap/default.asp' class='w3-hide-small' title='Bootstrap Tutorial'>BOOTSTRAP</a> 
    <a href='/jquery/default.asp' class='w3-hide-small' title='jQuery Tutorial'>JQUERY</a> 
    <a href='/angular/default.asp' class='w3-hide-small' title='Angular Tutorial'>ANGULAR</a> 
    <a href='/xml/default.asp' class='w3-hide-small' title='XML Tutorial'>XML</a> 
    

    DEMO:

    https://ideone.com/eFHU8n


    注:

    Why you shouldn't use regex to parse html

    +0

    1. Can you apply your regex in this link http://regexr.com/ ? 2. What is the alternative of using regex if I need certain tags from a long XHTML string ? – ohadinho

    +0

    Lobido now I can't get these kind of results: 'Color NCol ' (hrefは最初の属性ではありません) – ohadinho

    1

    更新されました。下記参照。 HTMLを解析する好ましい手段は、正規表現ではありませんが、HTMLパーサを持つこと

    // split the string up by anchor tags 
    // nested anchor tags is illegal, so this seems feasible: 
    var anchorArray = str.replace(/><a/g, '>¶<a').split('¶'); // ¶ is a placeholder to split 
    
    var matches = []; 
    var re = /<a .*href=["'].*\.asp["'].*?>.*?<\/a>/g; 
    
    // filter out the anchor elements with actual links in the final HTML 
    anchorArray.filter(function(element) { 
        if (re.test(element)) { 
         matches.push(element); // keep the match in an array (2nd condition) 
         return false; 
        } 
        else return true;  
    }); 
    
    var returnedHTML = anchorArray.join(''); // HTML w/o actual links (1st condition) 
    

    注:文字列形式でHTMLを使用している場合は

    は、あなたがこのような何かを行うことができます。

    +0

    この解決策は偽陽性の結果をもたらします:TUTORIALS ' – Adib

    +0

    Am I missing something? https://regex101.com/r/mN8zN2/2 It doesn't match. – timolawl

    +0

    Try this: https://regex101.com/r/mN8zN2/3 (directly from the example in the link by OP) – Adib

    0

    これは配列として変数試合にすべての一致を返します

    var matches = []; 
    
    input_content.replace(/[^<]*(<a href="([^"]+)">/w*<\a>)/g, function() { 
        matches.push(Array.prototype.slice.call(arguments, 1)) 
    }); 
    

    助けます!

    +0

    This only provides 4 matches. There are more links that qualify – Adib

    関連する問題