I'm trying to find a way to isolate a specific paragraph using a string as a starting point, where the string could be a word in any part of the line (not necessarily the end or the beginning).
So it will grab that entire line where the string occurs, and then it will grab until the line where it finds the secondary string. I've checked various questions and I'm not finding quite what I want. Here's an example input paragraph with the desired output paragraph:
Input:
JUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
NOTJUNK ABC NOTJUNK
DEF GHI JKL
MNO PQR STW
UVW XYZ NOTJUNK
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
Objective: I want to get every LINE from ABC (including the words before ABC anf after ABC in the same line) until XYZ (including the words before and after XYZ). ABC and XYZ will always only have one occurence in the paragraph - and ABC will always occur before XYZ. My paragraphs in questions are being obtained from emails, and I'm currently using PhpMimeMailParser to parse the email.
start string search term: ABC
end string search term: XYZ
Desired Output:
NOTJUNK ABC NOTJUNK
DEF GHI JKL
MNO PQR STW
UVW XYZ NOTJUNK
Glad I could help. Here is the regex which evidently does as you prescribed:
/.*(^.*ABC.*XYZ.*?[\r\n]).*/sm
Here is a regex tester link: regex test
Supporting info:
The multi line option m
is required since the capture needs to start at the beginning of a line and not the start of the string.
The single line option s
is required to ignore newlines with the dot.
So with the options as context, the expression can be described:
Ignore all characters until a line is found with ABC anywhere within the line. Begin to capture all characters starting at the line which contains the first ABC. Continue the capture until XYZ is found in a line. Stop the capture at the first newline found on the line with the XYZ. Ignore the remaining characters in string. The lazy qualifier in
.*?
ensures the match stops at the first newline (following the XYZ). I removed the{1}
from my original comment as it is unnecessary.