In the following regex:
EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line
I want to return a single match consisting for two lines:
include this line
and this as single match
I want to use EXCLUDE
as string identifying that the entire line should not be included.
edit: if I can get just the first match up to the line with "EXCLUDE" (or end of document whichever happens first), that would work too
CodePudding user response:
You can split the string on matches of the regular expression
^.*\bEXCLUDE\b.*\R
with global and multiline flags set.
In Ruby, for example, if the variable str
held the string
Firstly include this line
EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line
Lastly include this line
then the method String#split could be used to produce an array containing three strings.
str.split(/^.*\bEXCLUDE\b.*\R/)
#=> ["Firstly include this line",
# "include this line\nand this as single match",
# "Lastly include this line"]
Many languages have a method or function that is comparable to Ruby's split
.
The regular expression can be broken down as follows.
^ # 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
CodePudding user response:
With pcre you can use \K
to fotget what is matched so far, and first match the line containing exclude:
^.*\bEXCLUDE\b.*\K(?:\R(?!.*\bEXCLUDE\b).*)
If you want to match all lines that do not contain exclude, with consecutive lines:
(?:(?:^|\R)(?!.*\bEXCLUDE\b).*)
Or using a skip fail approach:
^.*\bEXCLUDE\b.*\R(*SKIP)(*F)|. (?:\R(?!.*\bEXCLUDE\b).*)*
CodePudding user response:
You could also match the lines with the EXCLUDE
and use it to split your text into blocks of what you are looking for:
<?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';
$pattern = '/^.*\bEXCLUDE\b.*$/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) {
$block = preg_replace('/^[\r\n] |[\r\n] $/', '', $block);
}
);
var_dump($desired_blocks);
Demo here: https://onlinephp.io/c/e70f4