gpt4 book ai didi

php - 正则表达式匹配每个字符和换行符

转载 作者:行者123 更新时间:2023-12-04 04:56:28 26 4
gpt4 key购买 nike

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

最佳答案

说明

您可以使用强制点匹配所有新行字符的 '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
)

)

关于php - 正则表达式匹配每个字符和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com