php preg_match s和m修饰符不适用于多行

时间:2015-07-13 15:13:40

标签: php regex preg-match

我有以下输入字符串,其中包含多行:

BYTE    $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$13,$14,$01,$19,$20,$01,$20,$17,$08,$09,$0C,$05,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66 // comment
BYTE    $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66

我使用以下preg_match语句来匹配数据部分(所以只有十六进制值)而不是前面的空格和文本,也不是尾随的空格和注释部分:

preg_match('/(\$.*?) /s', $sFileContents, $aResult);

输出是这样的:

output: Array
(
    [0] => $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$13,$14,$01,$19,$20,$01,$20,$17,$08,$09,$0C,$05,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66 
    [1] => $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$13,$14,$01,$19,$20,$01,$20,$17,$08,$09,$0C,$05,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66
)

正如您可能看到的那样,匹配似乎是正确的,但第一个输入行重复了两次。 ''修饰符应该帮助我超越行尾,但我似乎无法超越第一行。

有没有人知道如何继续?

2 个答案:

答案 0 :(得分:1)

您可以轻松匹配所有行的数据:

preg_match_all('/\$[\dA-Fa-f,\$]+/', $sFileContents, $aResult);
echo "<pre>".print_r($aResult,true);

输出:

$aResultArray:
(
    [0] => Array
        (
            [0] => $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$13,$14,$01,$19,$20,$01,$20,$17,$08,$09,$0C,$05,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66
            [1] => $66,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$66
        )

)

答案 1 :(得分:0)

您不需要selection(DOTALL)标志。您可以使用:

s

RegEx Demo