2

marc 21 タグは、次のようないくつかのドル記号 $ を含む行を内容とする場合があります。

$string='10$athis is text a$bthis is text b/$cthis is text$dthis is text d';

私はすべてのドルの歌を一致させ、各歌の後にテキストを取得しようとしました。私のコードは次のとおりです。

preg_match_all("/\\$[a-z]{1}(.*?)/", $string, $match);

出力は次のとおりです。

Array
(
    [0] => Array
        (
            [0] => $a
            [1] => $b
            [2] => $c
            [3] => $d
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

)

出力が次のようになるように、各歌の後にテキストをキャプチャする方法は次のとおりです。

Array
(
    [0] => Array
        (
            [0] => $a
            [1] => $b
            [2] => $c
            [3] => $d
        )

    [1] => Array
        (
            [0] => this is text a
            [1] => this is text b/
            [2] => this is text c
            [3] => this is text d
        )

)
4

2 に答える 2

3

正の先読みを使用して、\$文字通りまたは文字列の最後に一致させることができます

(\$[a-z]{1})(.*?)(?=\$|$)

正規表現のデモ

PHPコード

$re = "/(\\$[a-z]{1})(.*?)(?=\\$|$)/"; 
$str = "10\$athis is text a\$bthis is text b/\$cthis is text\$dthis is text d"; 
preg_match_all($re, $str, $matches);

イデオネデモ

:- 必要な結果は と にArray[1]ありArray[2]ます。Array[0]正規表現全体で見つかった一致用に予約されています。

于 2016-06-05T07:17:33.193 に答える
2

単純な正規表現で十分だと思います

$re = '/(\$[a-z])([^\$]*)/'; 
$str = "10\$athis is text a\$bthis is text b/\$cthis is text\$dthis is text d"; 
preg_match_all($re, $str, $matches);
print_r($matches);

デモ

于 2016-06-05T07:49:22.300 に答える