php正则表达式出错

时间:2013-06-06 15:15:30

标签: php regex preg-match preg-match-all

我有这段代码:

$text = "###12###hello###43###good###113###thefinalstring";
preg_match_all('/(.*?)###(\d*)###(.*?)/is', $text, $matches, PREG_SET_ORDER);

如果我转储$ match,为什么在任何地方都没有“thefinalstring”? 正则表达式中的错误在哪里?

由于

2 个答案:

答案 0 :(得分:1)

(.*?)###(\d*)###(.*?)([a-zA-Z]*)

使用此正则表达式

答案 1 :(得分:0)

尝试使用:

$text = "###12###hello###43###good###113###thefinalstring";
preg_match_all('/###(\d*)###([^#]*)/is', $text, $matches, PREG_SET_ORDER);
print_r($matches);

<强>输出:

Array
(
    [0] => Array
        (
            [0] => ###12###hello
            [1] => 12
            [2] => hello
        )

    [1] => Array
        (
            [0] => ###43###good
            [1] => 43
            [2] => good
        )

    [2] => Array
        (
            [0] => ###113###thefinalstring
            [1] => 113
            [2] => thefinalstring
        )

)