Is it possible to calculate with regex group matches?
String:
(00) Bananas
...
(02) Apples (red ones)
...
(05) Oranges
...
(11) Some Other Fruit
...
If the difference between the numbers at the beginning of each row is 3 or less, remove the “…” inbetween. So the string should be returned like this:
(00) Bananas
(02) Apples (red ones)
(05) Oranges
...
(11) Some Other Fruit
Regex:
$match = '/(*ANYCRLF)\((\d+)\) (.+)$
\.{3}
\((\d+)\) (.+)/m';
Now the tricky part is how to grab the matches and add some to a condition like
if($3-$1 >= 3) {
//replace
}
Test: http://codepad.viper-7.com/f6iI4m
Thanks!
Here’s how you could do it with
preg_replace_callback().Here’s the pattern in pieces:
Thus, the entire pattern’s match is the first two lines (i.e., numbered line and ‘…’ line).
If the difference in numbers is greater than 3, replace with original text in
$match[0](i.e., no change). If difference is less than or equal to 3, replace with first line only (found in$match1]).