RE: [PHP] Non-Greedy Regex Matching
| From: | WILSON, Nick | Date: | Tue, 31 Oct 2000 22:15:12 +0000 |
| Subject: | RE: [PHP] Non-Greedy Regex Matching | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-23021@lists.php.net to get a copy of this message | ||
I'd try something like
'<pmlink>([^<]?+)</pmlink>',
or
'<pmlink>([^<]+)?</pmlink>',
or else use the
preg_replace which is more perl like in it's reg exp.
Nick
> -----Original Message-----
> From: Matt Henderson [mailto:matt@makalumedia.com]
> Sent: Tuesday, 31 October 2000 21:11
> To: PHP Discussion List
> Subject: [PHP] Non-Greedy Regex Matching
>
>
> 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
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
>