Home > Backend Development > PHP Tutorial > 请问正则必须包含怎么匹配呢

请问正则必须包含怎么匹配呢

WBOY
Release: 2016-06-13 12:33:21
Original
697 people have browsed it

请教正则必须包含如何匹配呢
正则:



内容:

  • str
  • 标题一
  • 标题二


    目标:(必须包含
    )
    标题二



    当前错误结果:(匹配成了整段了)
    标题一
    标题二
    内容:
  • str
  • ..." data-pics="">
    ------解决方案--------------------
    加上 U,禁止贪婪
    ------解决方案--------------------
    是这个意思吗
    <br />
    $str = '<li>str</li><li><a href="/x.html">标题一</a></li><a href="/BAMC"><div class="plst"><b>标题二</b></div></a>';<br />
    preg_match_all("/<a.*?><div class=\"plst\"><b>(.*)<\/b><\/div><\/a>/is",$str,$match);<br />
    echo "<pre class="brush:php;toolbar:false">";<br />
    print_r($match);<br />
    
    Copy after login

    ------解决方案--------------------
    给个提醒
    匹配一些有边界的内容,应以 边界+排除边界+边界 这样的形式写正则,避免随意地用圆点
    尤其是html等标记语言,因为标记语言的特性就是标记,中间含有换行也是合法的,圆点不能涵盖,随便用圆点就有点想当然认为源字串一定工整了
    举个极端例子如:
    href="
    abc/abc.html">
    ABC

    这段html字串完全合法,一点都不影响网页的显示,但正则就要想好了

    "[^"]*"
    <[^>]+>
    [^<]*

    ------解决方案--------------------
    $s =<<< TXT<br />
    <li>str</li><li><a href="/x.html">标题一</a></li><a href="/BAMC"><div class="plst"><b>标题二</b></div></a><br />
    TXT;<br />
    preg_match('/.+(<a.+?<div class="plst".+?a>)/is', $s, $m);<br />
    echo $m[1];
    Copy after login
    <a href="/BAMC"><div class="plst"><b>标题二</b></div></a><br />
    
    Copy after login
    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template