2

我想使用正则表达式过滤一些数据。至于现在我有一些超过 2 行的文本,我试图使换行符与[^.*]. 但它似乎没有通过换行符,因此它与第二行的任何结果都不匹配。如何包含换行符?我尝试了类似的方法[^\n\r.*],但没有成功。

4

1 回答 1

3

描述

您可以使用 's' 选项强制点匹配所有换行符,或者如果您无法控制底层代码,您可以尝试:

([^.]|[.])

这将匹配每个字符。点有时与回车、换行不匹配。

PHP 示例

<?php
$sourcestring="This is my.
super cool
test string";
preg_match_all('/([^.]|[.])/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => Array
        (
            [0] => T
            [1] => h
            [2] => i
            [3] => s
            [4] =>  
            [5] => i
            [6] => s
            [7] =>  
            [8] => m
            [9] => y
            [10] => .
            [11] => 
            [12] => 

            [13] => s
            [14] => u
            [15] => p
            [16] => e
            [17] => r
            [18] =>  
            [19] => c
            [20] => o
            [21] => o
            [22] => l
            [23] => 
            [24] => 

            [25] => t
            [26] => e
            [27] => s
            [28] => t
            [29] =>  
            [30] => s
            [31] => t
            [32] => r
            [33] => i
            [34] => n
            [35] => g
        )

    [1] => Array
        (
            [0] => T
            [1] => h
            [2] => i
            [3] => s
            [4] =>  
            [5] => i
            [6] => s
            [7] =>  
            [8] => m
            [9] => y
            [10] => .
            [11] => 
            [12] => 

            [13] => s
            [14] => u
            [15] => p
            [16] => e
            [17] => r
            [18] =>  
            [19] => c
            [20] => o
            [21] => o
            [22] => l
            [23] => 
            [24] => 

            [25] => t
            [26] => e
            [27] => s
            [28] => t
            [29] =>  
            [30] => s
            [31] => t
            [32] => r
            [33] => i
            [34] => n
            [35] => g
        )

)
于 2013-05-22T16:42:23.483 回答