Non-Greedy Regex Matching
| From: | Matt Henderson | Date: | Tue, 31 Oct 2000 10:11:27 +0000 |
| Subject: | Non-Greedy Regex Matching | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-22895@lists.php.net to get a copy of this message | ||
Hello,
I'm trying to replace the contents of a custom tag <pmlink>content</pmlink>
with the following expression:
$OUT = ereg_replace(
'<pmlink>([^<]+)</pmlink>',
$linkTemplate,
$string);
This works except that the match is 'greedy'-- e.g. the 'content' of the
following string:
<pmlink>Hello</pmlink> world <pmlink>again</pmlink>
...would incorrectly be determined as...
Hello</pmlink> world <pmlink>again
I tried the Perl modifier '?' as follows:
$OUT = ereg_replace(
'<pmlink>([^<]+?)</pmlink>',
$linkTemplate,
$string);
...and PHP4 returned a "bad regular expression" error. Does anyone on the
list know what modifier I can use to have a non-greedy matching?
Many thanks! (PS: Please copy any responses to my email address, as I'm on
the road and have very limited net access.)
-- Matt Henderson
--
MAKALUMEDIA INTERNET & ENGINEERING SERVICES GMBH
matt@makalumedia.com | http://www.makalumedia.com/
Robert-Bosch Strasse 7, 64293 Darmstadt, Germany
Tel: +49 6151 872 4600, Fax: +49 6151 872 4619