Page 1 of 1

greedy quantifier issue?

Posted: Sun Apr 26, 2009 5:09 pm
by SidewinderX
I am trying to remove comments from a piece of code. For example:

Code: Select all

/* Header */
This is my header
/* Footer */
This is my footer
/* EOF */

would become

Code: Select all

This is my header
This is my footer
I tried

Code: Select all

$content = preg_replace("/\/\*.*\*\//", "", $content);

This *works* but it matches the /* before Header and the last */ before EOF which causes the entire file to be replaced with nothing. How can I craft my expression to get the results I expect?


Thanks,
John

Re: greedy quantifier issue?

Posted: Mon Apr 27, 2009 12:31 am
by prometheuzz
Try:

Code: Select all

$content = preg_replace("@/\*.*?\*/@s", "", $content);

Re: greedy quantifier issue?

Posted: Sat May 02, 2009 8:16 am
by david64
By default PRCE, which powers the preg functions is set to greedy. You can turn this off using the U modifer like so:

Code: Select all

$content = preg_replace('/expression/U', '', $content);

Re: greedy quantifier issue?

Posted: Sat May 02, 2009 9:29 am
by prometheuzz
@david64

Impressive website you have. Nice and clean, I like it a lot!

Re: greedy quantifier issue?

Posted: Sat May 02, 2009 12:26 pm
by david64
Thanks for your kind words.

I have got to the stage of being bored with the design now and wanting to change :|

Re: greedy quantifier issue?

Posted: Sat May 02, 2009 12:47 pm
by prometheuzz
david64 wrote:Thanks for your kind words.

I have got to the stage of being bored with the design now and wanting to change :|
Snob!
;)

Re: greedy quantifier issue?

Posted: Sat May 02, 2009 4:04 pm
by SidewinderX
Thanks guys, both solutions work. Is one better than the other?

Re: greedy quantifier issue?

Posted: Sun May 03, 2009 2:20 am
by prometheuzz
SidewinderX wrote:Thanks guys, both solutions work.
Glad to hear it.
SidewinderX wrote:Is one better than the other?
No, not really. Davids proposal will make all greedy operators in the regex reluctant. So this regex:

Code: Select all

'/.+abc.+/U'
equals

Code: Select all

'/.+?abc.+?/' // both are made reluctant
while mine will make just one operator reluctant.