I have a text like:
This patch requires:
Patch 1.10-1
Patch 1.11-2
Notes:
I want to extract
Patch 1.10-1
Patch 1.11-2
with the following regex:
This patch requires\:[\r\n](.*)[\r\n]Notes\:
But nothing is matched.
Why ?
I have a text like:
This patch requires:
Patch 1.10-1
Patch 1.11-2
Notes:
I want to extract
Patch 1.10-1
Patch 1.11-2
with the following regex:
This patch requires\:[\r\n](.*)[\r\n]Notes\:
But nothing is matched.
Why ?
For the sake of alternative solutions:
^\QThis patch requires:\E\R+
\K
(?:^Patch.+\R)+
^\QThis patch requires:\E # match "This patch requires:" in one line and nothing else
\R+ # match empty lines + newlines
\K # "Forget" what's left
(?:^Patch.+\R)+ # match lines that start with "Patch"
In PHP
:
<?php
$regex = '~
^\QThis patch requires:\E\R+
\K
(?:^Patch.+\R)+
~xm';
if (preg_match_all($regex, $your_string_here, $matches)) {
// do sth. with matches
}
See a demo on regex101.com (and mind the verbose
and multiline
modifier!).
What about (?<=[\r\n])(.+)(?=[\r\n])
But nothing is matched.
DOT .
doesn't match newline characters so .*
doesn't go beyond one line. For that you need to set DOTALL s
modifier on or try an alternative [\s\S]
(which literally means any character without any exception).
preg_match('~This patch requires:\s*([\s\S]*?)\s*^Notes:~m', $text, $matches);
echo $matches[1];
Note: don't use greedy-dot (.*
) if multiple matches are going to occur instead go lazy .*?
By the way this is supposed to be the most efficient regex you may come across:
This patch requires:\s+((?:(?!\s*^Notes:).*\R)*)