Home  >  Article  >  Backend Development  >  跪求正则表达式!该如何解决

跪求正则表达式!该如何解决

WBOY
WBOYOriginal
2016-06-13 13:30:54906browse

跪求正则表达式!!!
求正则表达式提取第一个url(直接取出最后那个id最佳),超链接的内容,提问日期那个时间,后面那个超链接内容,以及回答数那个数字,这个要preg_match_all循环匹配的,各位大牛帮帮小弟啊!




  • 我擦我擦我擦擦擦
    我擦我擦我擦擦擦



    提问日期:2012/05/12 18:06:58 - kagakugizyutugakari我擦 - 回答数:0 - 悬赏:我又擦50

    我擦 > 我擦 > 我擦




  • ------解决方案--------------------
    结果出来了,就是回朔多了点~凑合用吧
    PHP code
    <?php $str=<<<HTML
    <li>
    
    我擦我擦我擦擦擦。 我擦我擦我擦擦擦
    提问日期:2012/05/12 18:06:58 - kagakugizyutugakari我擦 - 回答数:0 - 悬赏:我又擦50
    我擦 > 我擦 > 我擦
    HTML; $pattern='/ question_detail\/([^"]+)"> #第一个id (.+?) #第一个链接内容 .+?提问日期:([^-]+).+? #提问时间 ]+) #第二个链接内容 .+?回答数:(\d+) #回答数 /xis'; preg_match_all($pattern,$str,$matches); print_r($matches); /* Array ( [0] => Array ( [0] => question_detail/q1187216696">我擦我擦我擦擦擦。 我擦我擦我擦擦擦 提问日期:2012/05/12 18:06:58 - kagakugizyutugakari我擦 - 回答数:0 ) [1] => Array ( [0] => q1187216696 ) [2] => Array ( [0] => 我擦我擦我擦擦擦。 我擦我擦我擦擦擦 ) [3] => Array ( [0] => 2012/05/12 18:06:58 ) [4] => Array ( [0] => kagakugizyutugakari ) [5] => Array ( [0] => 0 ) )*/
    ------解决方案--------------------
    PHP code
    [User:root Time:18:44:25 Path:/home/liangdong/php]$ php preg.php 
    Array
    (
        [0] => <a href="http://url/question_detail/q1187216696">我擦我擦我擦擦擦<wbr>。我擦我擦我擦擦擦<wbr></wbr></wbr></a>
        [1] => q1187216696
    )
    Array
    (
        [0] => 
    提问日期:2012/05/12 18:06:58 - kagakugizyutugakari [1] => 2012/05/12 18:06:58 [2] => kagakugizyutugakari ) [User:root Time:18:44:26 Path:/home/liangdong/php]$ cat preg.php
    我擦我擦我擦擦擦。我擦我擦我擦擦擦
    提问日期:2012/05/12 18:06:58 - kagakugizyutugakari我擦 - 回答数:0 - 悬赏:我又擦50
    我擦 > 我擦 > 我擦
    EOF; $nmatches = preg_match('/.*/Uis', $str, $matches); print_r($matches); $nmatches = preg_match('/
    .*(\d{4}\S+\s+\S+)\s*-\s*.*(.*)/Uis', $str, $matches); print_r($matches); ?>
    Statement:
    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