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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
binarybuddha
Forum Newbie
Posts: 2
Joined: Sun Nov 02, 2003 4:50 am

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

Post 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.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post 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
binarybuddha
Forum Newbie
Posts: 2
Joined: Sun Nov 02, 2003 4:50 am

Post by binarybuddha »

http://weitz.de/files/regex-coach.exe
Executable windows installer
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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);
?>
Post Reply