On Wed, Feb 08, 2012 at 10:06:50AM +0000, Brett Parker wrote:
On 07 Feb 18:40, Chris Green wrote:
[big snip]
brettp@erwin:~/temp/cg$ cat test.php <?php $filename = "test.txt"; $fh = fopen($filename, "r"); $data = fread($fh, filesize($filename)); $hasmatch = preg_match("/(^|[\r\n]+)/x[\r\n]+(.*?)[\r\n]+/[\r\n]+/s", $data, $matches);
Now that's just silly! :-)
print $data; print "====\n"; if ($hasmatch > 0) { print $matches[2]; print "\n"; }
?> brettp@erwin:~/temp/cg$ php5 test.php /x Line one Line two / Line three / Not a line that should be in the output / Another line that should not be in the output / ==== Line one Line two / Line three brettp@erwin:~/temp/cg$
Hope that gives you the pointer you need.
It turns out that one can turn on 'ungreedy' very simply *within* the RE. My code is actually in a wiki plugin, several layers away from the actual PHP preg_match(), the delimiters are added between my call and the preg_match().
After digging around in the PHP PCRE documentation I found that what I needed was:-
'(?U)^/x.*/\n'
which works perfectly so far, allowing embedded / characters but stopping at the first end-of-line /. It's the (?U) that tells the RE to be ungreedy. I can't do the standard U after the final delimiter because the delimiters get added later.