Re: Non-Greedy Regex Matching
| From: | Monte Ohrt | Date: | Tue, 31 Oct 2000 14:48:34 +0000 |
| Subject: | Re: Non-Greedy Regex Matching | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-22941@lists.php.net to get a copy of this message | ||
Hi Matt,
Give this a try:
$OUT = preg_replace(
'|<pmlink>([^<]+)</pmlink>|U',
$linkTemplate,
$string);
Put an "i" next to the "U" if you want it case-insensitive as well as
ungreedy.
Matt Henderson wrote:
>
> 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
--
Monte Ohrt <monte@ispi.net>
http://www.ispi.net/