quite new to regexes i'm trying to optimize one, or at least know if there are better ways to do it.
Here is my input string:
$str = 'Some text
spanned on
several lines
txt_to_grab1 fixed_text1 txt_to_grab2
Full line to grab
txt_to_grab3 fixed_text2 txt_to_grab4
Some text after';
I'm trying to grab the lines from "txt_to_grab1" to "txt_to_grab4", but only the words "txt_to_grabX" and the line "Full line to grab".
I want to preserve everything untouched before and after (ie line breaks), but remove line breaks inside the lines i grab (as each line will be a <tr>
that'll go into an html table).
Regex patterns/replace i found matching:
$find = "#(?<=
)(.*?) fixed_text1 (.*?)(
.*?
)(.*?) fixed_text2 (.*?)(
)#i";
$replace = '"$1" && "$2" grabbed.$3"$4" && "$5" grabbed.$6';
$find = "#(.*)(?<=
)(.*?) fixed_text1 (.*?)(
)(.*)(?<=
)(.*?) fixed_text2 (.*?)(
.*)#is";
$replace = '$1"$2" && "$3" grabbed.$4$5"$6" && "$7" grabbed.$8';
Questions :
All questions can be sum up as : are there better/shorter/faster patterns ?
-
how to make the patterns work with either or ? I read somewhere on stack that (? ) would be a solution, but i dunno how to use them in lookbehinds. For example the following patterns work, but i don't like them (dirty as only are used in lookbehinds, may produce unexpected results):
"#(?<= )(.*?) fixed_text1 (.*?)(? .*?? )(.*?) fixed_text2 (.*?)(? )#i" "#(.*)(?<= )(.*?) fixed_text1 (.*?)(? )(.*)(?<= )(.*?) fixed_text2 (.*?)(? .*)#is";
even better, how to use the "s" modifier to remove all line breaks from the pattern, so being able to use (.*?) but still grabbing what i want ? Word boundaries ?
is the multiline mode (m modifier) useful/helpful here ?
I'd really like the regexes to be explained, if you provide some :)