在 php 中使用 preg_replace 删除许多行


Deleting many lines using preg_replace in php

虽然正则表达式似乎是一个反复出现的麻烦,但我没有找到解决问题的方法。

我有一个包含许多行的文件(> 75 000),我想在其中替换其中的许多行,所有这些都在两个可识别的模式(starting_point和ending_point)之间。我的正则表达式尝试:

$filtered = preg_replace('%(#'sstarting_point).+(#'sending_point)%isU',$replacement,$2befiltered);

现在是任何字符,包括 '。我用了%..% 作为分隔符,因为我的行几乎有任何内容,但没有 %(任何内容都包括/,$,-,",空格,+,{,}),并且我遇到了"编译失败:偏移时没有什么可重复的......"。

但我似乎无法让它工作。我想也许 php 中的 pcre .ini 是问题所在,但似乎并非如此。我尝试使用 ?U,没有成功。与 .* 相同?正如我在某些地方读到的那样。也许我只是写错了,但是..

无论如何,有人愿意为此挠头吗?

文本示例(与...表示许多其他行):

whatever
lines
you
can
...
# starting_point
lines/*-
to$_,"
delete+{}=@
...
# ending_point
some
other
lines
...

似乎有效:%(#'sstarting_point)(.*?)(#'sending_point)%isU

$matches [2] [0] 包含介于 # starting_point 和 # ending_point 之间的行

$filtered = preg_replace('/(#'sstarting_point)(.*?)(#'sending_point)/sm', $replacement,$2befiltered);

试试这个。