Page 1 of 1

PHP RegEX Lookbehind (?<=str) non-functional

Posted: Sun Nov 02, 2003 4:50 am
by binarybuddha
Trying to use Regular Expressions to extract a pieces of a string given string startX and endY, then grab everything between.
i.e.
~~~~~ startX _grab this unknown content_ endY ~~~~~~~

Sample Code
<?
$string ="Hello World and Goodbye World"; //Attempting to retrieve-> World and Goodbye

//___________\/ This should tell the RegEx to start looking here, but it isn't working.....
preg_match("/(?<=Hello ).*(?= World)/" , $string , $World_and_Goodbye);
echo "Matched: " . $World_and_Goodbye[0] . "<br>\n";

//__________\/ This kind of works, but you have to include that that char in the output, then strip it...
preg_match("/o .* (?=World)/i" , $string , $World_and_Goodbye);
echo "Matched: " . $World_and_Goodbye[0] . "<br>\n";
?>

The lookbehind seems to nullify any match attempt, while the lookahead is working flawlessly.

Image
RegEX Coach says I'm not insane

Am I mis-interpreting the function of the lookbehind, or is there a better way to extract unknown info between two known strings?

I thank you for your time, and greatly appreciate any response/input you might have.

Posted: Sun Nov 02, 2003 5:19 am
by devork
To test the RE's,I tried to download RE Coach but no luck
http://weitz.de/regex-coach/ is not working ....
all search goes to only this url any mirror

Posted: Sun Nov 02, 2003 12:35 pm
by binarybuddha
http://weitz.de/files/regex-coach.exe
Executable windows installer

Posted: Sun Nov 02, 2003 1:11 pm
by Cruzado_Mainfrm
try

Code: Select all

<?php
$string="Hello World and Goodbye World";
$startWord="Hello";
$endWord="World";
preg_match("/^".$startWord."(.*)".$endWord."$/si",$string,$resultArray);
print_r($resultArray);
?>