正则表达式多行匹配,不包括包含字符串的行



在以下正则表达式中:

EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line

我想返回一个包含两行的匹配:

include this line
and this as single match

我想使用EXCLUDE作为字符串,标识不应该包含整行。

编辑:如果我能得到第一个匹配到"EXCLUDE"(或文档结束,以先发生的为准),也可以

对于pcre,您可以使用K来忘记到目前为止匹配的内容,并首先匹配包含exclude的行:

^.*bEXCLUDEb.*K(?:R(?!.*bEXCLUDEb).*)+

Regex演示

如果要用连续的行匹配不包含exclude的所有行:

(?:(?:^|R)(?!.*bEXCLUDEb).*)+

Regex演示

或者使用跳过失败方法:

^.*bEXCLUDEb.*R(*SKIP)(*F)|.+(?:R(?!.*bEXCLUDEb).*)*

Regex演示

您可以根据正则表达式

的匹配对字符串进行拆分
^.*bEXCLUDEb.*R

设置全局和多行标志。

例如,在Ruby中,如果变量str包含字符串
Firstly include this line
EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line
Lastly include this line

则可以使用string# split方法生成包含三个字符串的数组。

str.split(/^.*bEXCLUDEb.*R/)
#=> ["Firstly include this line",
#    "include this linenand this as single match",
#    "Lastly include this line"]

许多语言都有类似Ruby的split的方法或函数。

演示正则表达式可以分解如下:

^        # match the beginning of a line
.*       # match zero or more characters other than line
# terminators, as many as possible
b       # match word boundary
EXCLUDE  # match literal
b       # match word boundary
.*       # match zero or more characters other than line
# terminators, as many as possible
R       # match line terminator

您也可以将行与EXCLUDE匹配,并使用它将您的文本分割成您正在寻找的块:

<?php
$input = 'First include this line
EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line
Lastly include this line';
// ^ matches the beginning of a line.
// .* matches anything (except new lines) zero or multiple times.
// b matches a word boundary (to avoid matching NOEXCLUDE).
// $ matches the end of a line.
$pattern = '/^.*bEXCLUDEb.*$/m';
// Split the text with all lines containing the EXCLUDE word.
$desired_blocks = preg_split($pattern, $input);
// Get rid of the new lines around the matched blocks.
array_walk(
$desired_blocks,
function (&$block) {
// R matches any Unicode newline sequence.
// ^ matches the beginning of the string.
// $ matches the end of the string.
// | = or
$block = preg_replace('/^R+|R+$/', '', $block);
}
);
var_export($desired_blocks);

此处演示:https://onlinephp.io/c/4216a

输出:

array (
0 => 'First include this line',
1 => 'include this line
and this as single match',
2 => 'Lastly include this line',
)

最新更新