php regular expression doesn"t match

php regular expression doesn"t match

am 24.10.2007 08:50:48 von cmk128

Hi
PHP's regular expression look like doesn't support .*? syntax. So i
cannot match the shortest match. For exmaple:

$str="a1b a3b";
$str1=ereg_replace("a.*b", "peter", $str1);
will produce "peter", but i want "peter peter", so how to?

thanks
from Peter (cmk128@hotmail.com)

Re: php regular expression doesn"t match

am 24.10.2007 10:21:44 von Erwin Moller

cmk128@hotmail.com wrote:
> Hi

Hi,

> PHP's regular expression look like doesn't support .*? syntax. So i
> cannot match the shortest match. For exmaple:
>
> $str="a1b a3b";

Typo. That was $str1="a1b a3b" I expect.

> $str1=ereg_replace("a.*b", "peter", $str1);
> will produce "peter", but i want "peter peter", so how to?
>

Yes, * is greedy.
I do not know your real-world example, but maybe using a wordboundary
can solve your problem?
eg:
$str1=ereg_replace("/a.*b\b/", "peter", $str1);

> thanks
> from Peter (cmk128@hotmail.com)
>

Regards,
Erwin Moller

Re: php regular expression doesn"t match

am 24.10.2007 11:05:54 von Erwin Moller

Erwin Moller wrote:
> cmk128@hotmail.com wrote:
>> Hi
>
> Hi,
>
>> PHP's regular expression look like doesn't support .*? syntax. So i
>> cannot match the shortest match. For exmaple:
>>
>> $str="a1b a3b";
>
> Typo. That was $str1="a1b a3b" I expect.
>
>> $str1=ereg_replace("a.*b", "peter", $str1);
>> will produce "peter", but i want "peter peter", so how to?
>>
>
> Yes, * is greedy.
> I do not know your real-world example, but maybe using a wordboundary
> can solve your problem?
> eg:
> $str1=ereg_replace("/a.*b\b/", "peter", $str1);

That is nonsense. (Erwin had a coffee now.)
It doesn't solve the greedinessproblem.
Excuse me for the noise.

A better solution would be to explode the string first on space, and use
a regexpr to modify if matched.

Regards,
Erwin Moller

>
>> thanks
>> from Peter (cmk128@hotmail.com)
>>
>
> Regards,
> Erwin Moller

Re: php regular expression doesn"t match

am 24.10.2007 11:33:17 von Erwin Moller

cmk128@hotmail.com wrote:
> Hi
> PHP's regular expression look like doesn't support .*? syntax. So i
> cannot match the shortest match. For exmaple:
>
> $str="a1b a3b";
> $str1=ereg_replace("a.*b", "peter", $str1);
> will produce "peter", but i want "peter peter", so how to?
>
> thanks
> from Peter (cmk128@hotmail.com)
>

Hi Peter,

The coffe sunk in, and I gave it a new try:

What about this?

$str="a1b a3b";
$str=preg_replace("/a[^b]*b/", "peter", $str);

It matches a, then any non-b character as many times as possible, and
then the b itself.
Effectively demanding nongreediness (I think).

Seems to work here.

Regards,
Erwin Moller

Re: php regular expression doesn"t match

am 24.10.2007 12:11:34 von luiheidsgoeroe

On Wed, 24 Oct 2007 08:50:48 +0200, wrote:

> Hi
> PHP's regular expression look like doesn't support .*? syntax. So i=

> cannot match the shortest match. For exmaple:
>
> $str=3D"a1b a3b";
> $str1=3Dereg_replace("a.*b", "peter", $str1);
> will produce "peter", but i want "peter peter", so how to?

use the preg_* functions

$str=3D"a1b a3b";
echo preg_replace("/a.*?b/", "peter", $str);
?>
-- =

Rik Wasmus

Re: php regular expression doesn"t match

am 24.10.2007 12:18:53 von Captain Paralytic

On 24 Oct, 11:11, "Rik Wasmus" wrote:
> On Wed, 24 Oct 2007 08:50:48 +0200, wrote:
> > Hi
> > PHP's regular expression look like doesn't support .*? syntax. So i
> > cannot match the shortest match. For exmaple:
>
> > $str="a1b a3b";
> > $str1=ereg_replace("a.*b", "peter", $str1);
> > will produce "peter", but i want "peter peter", so how to?
>
> use the preg_* functions
>
> > $str="a1b a3b";
> echo preg_replace("/a.*?b/", "peter", $str);
> ?>
> --
> Rik Wasmus

Mine was similar:
$strl = preg_replace('/(a.*b)*/','Peter',$strl);

Re: php regular expression doesn"t match

am 24.10.2007 13:56:06 von Andy Hassall

On Tue, 23 Oct 2007 23:50:48 -0700, cmk128@hotmail.com wrote:

> PHP's regular expression look like doesn't support .*? syntax. So i
>cannot match the shortest match. For exmaple:
>
>$str="a1b a3b";
>$str1=ereg_replace("a.*b", "peter", $str1);
>will produce "peter", but i want "peter peter", so how to?

PHP supports two regular expression libraries, neither of which are "PHP
regular expressions" - there's POSIX expressions, and Perl-compatible
expressions (PCRE).

The manual says that the ereg functions (the POSIX ones) are to be avoided in
favour of the PCRE ones which are better so many ways.

.*? is a PCRE construct (match zero-or-more any character, greediness inverted
[see also U modifier]), so use the right function - preg_replace.

http://uk3.php.net/manual/en/ref.regex.php
http://uk3.php.net/manual/en/ref.pcre.php
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Re: php regular expression doesn"t match

am 24.10.2007 15:18:59 von Steve

"Erwin Moller"
wrote in
message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl...
> Erwin Moller wrote:
>> cmk128@hotmail.com wrote:
>>> Hi
>>
>> Hi,
>>
>>> PHP's regular expression look like doesn't support .*? syntax. So i
>>> cannot match the shortest match. For exmaple:
>>>
>>> $str="a1b a3b";
>>
>> Typo. That was $str1="a1b a3b" I expect.
>>
>>> $str1=ereg_replace("a.*b", "peter", $str1);
>>> will produce "peter", but i want "peter peter", so how to?
>>>
>>
>> Yes, * is greedy.
>> I do not know your real-world example, but maybe using a wordboundary
>> can solve your problem?
>> eg:
>> $str1=ereg_replace("/a.*b\b/", "peter", $str1);
>
> That is nonsense. (Erwin had a coffee now.)
> It doesn't solve the greedinessproblem.
> Excuse me for the noise.
>
> A better solution would be to explode the string first on space, and use
> a regexpr to modify if matched.

uhhh...bullshit. first, there is no need. second, you assume you know what
he wants based on the test string. be logical! his string may very well be
'a1ba2ba3b'. if he becomes more specific with us about what he wants, THEN
you'll be able to make such leaps...and be a bit more accurate. your 'better
solution' is tripe. as others have pointed out:

preg_replace('/a.*?b/', 'peter', 'a1ba2ba3b');

with heavy emphasis on PREG...THAT is the only solution warranting
attention.

you seem to think coffee helps you out. i recommend you go make two or three
more pots.

Re: php regular expression doesn"t match

am 24.10.2007 16:07:36 von Erwin Moller

Steve wrote:
> "Erwin Moller"
> wrote in
> message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl...
>> Erwin Moller wrote:
>>> cmk128@hotmail.com wrote:
>>>> Hi
>>> Hi,
>>>
>>>> PHP's regular expression look like doesn't support .*? syntax. So i
>>>> cannot match the shortest match. For exmaple:
>>>>
>>>> $str="a1b a3b";
>>> Typo. That was $str1="a1b a3b" I expect.
>>>
>>>> $str1=ereg_replace("a.*b", "peter", $str1);
>>>> will produce "peter", but i want "peter peter", so how to?
>>>>
>>> Yes, * is greedy.
>>> I do not know your real-world example, but maybe using a wordboundary
>>> can solve your problem?
>>> eg:
>>> $str1=ereg_replace("/a.*b\b/", "peter", $str1);
>> That is nonsense. (Erwin had a coffee now.)
>> It doesn't solve the greedinessproblem.
>> Excuse me for the noise.
>>
>> A better solution would be to explode the string first on space, and use
>> a regexpr to modify if matched.
>
> uhhh...bullshit.
first, there is no need. second, you assume you know what
> he wants based on the test string. be logical! his string may very well be
> 'a1ba2ba3b'. if he becomes more specific with us about what he wants, THEN
> you'll be able to make such leaps...and be a bit more accurate. your 'better
> solution' is tripe. as others have pointed out:
>
> preg_replace('/a.*?b/', 'peter', 'a1ba2ba3b');
>
> with heavy emphasis on PREG...THAT is the only solution warranting
> attention.
>
> you seem to think coffee helps you out. i recommend you go make two or three
> more pots.
>


Bullshit? More pots of coffee?

If you want to appear smart, you better first read the other response I
wrote in this same thread many hours ago.
It contained a better solution. One that actually works and solves the
OP problem.

I fixed my own nonsense with a good solution that works in the other
response.
Will you do the same and fix the insulting crap you wrote about me in here?

Erwin Moller

Re: php regular expression doesn"t match

am 24.10.2007 19:31:03 von Steve

"Erwin Moller"
wrote in
message news:471f52a9$0$241$e4fe514c@news.xs4all.nl...
> Steve wrote:
>> "Erwin Moller"
>> wrote in
>> message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl...
>>> Erwin Moller wrote:
>>>> cmk128@hotmail.com wrote:
>>>>> Hi
>>>> Hi,
>>>>
>>>>> PHP's regular expression look like doesn't support .*? syntax. So i
>>>>> cannot match the shortest match. For exmaple:
>>>>>
>>>>> $str="a1b a3b";
>>>> Typo. That was $str1="a1b a3b" I expect.
>>>>
>>>>> $str1=ereg_replace("a.*b", "peter", $str1);
>>>>> will produce "peter", but i want "peter peter", so how to?
>>>>>
>>>> Yes, * is greedy.
>>>> I do not know your real-world example, but maybe using a wordboundary
>>>> can solve your problem?
>>>> eg:
>>>> $str1=ereg_replace("/a.*b\b/", "peter", $str1);
>>> That is nonsense. (Erwin had a coffee now.)
>>> It doesn't solve the greedinessproblem.
>>> Excuse me for the noise.
>>>
>>> A better solution would be to explode the string first on space, and use
>>> a regexpr to modify if matched.
>>
>> uhhh...bullshit.
> first, there is no need. second, you assume you know what
>> he wants based on the test string. be logical! his string may very well
>> be 'a1ba2ba3b'. if he becomes more specific with us about what he wants,
>> THEN you'll be able to make such leaps...and be a bit more accurate. your
>> 'better solution' is tripe. as others have pointed out:
>>
>> preg_replace('/a.*?b/', 'peter', 'a1ba2ba3b');
>>
>> with heavy emphasis on PREG...THAT is the only solution warranting
>> attention.
>>
>> you seem to think coffee helps you out. i recommend you go make two or
>> three more pots.
>
>
> Bullshit? More pots of coffee?
>
> If you want to appear smart, you better first read the other response I
> wrote in this same thread many hours ago.
> It contained a better solution. One that actually works and solves the OP
> problem.

oh, you mean this:

/a[^b]*b/

still missing a ? after the * ... unless you want mixed results.

/a[^b]*?b/

is appropriate. it may have worked with the test string, but not hardly a
catch-all in the real world. since the op was confused about *?, your
snippet pattern doesn't server to clear any of that up.


> I fixed my own nonsense with a good solution that works in the other
> response.
> Will you do the same and fix the insulting crap you wrote about me in
> here?

no...but i did justify my comment further. ;^)

Re: php regular expression doesn"t match

am 25.10.2007 10:55:02 von Erwin Moller

Steve wrote:



>>
>> Bullshit? More pots of coffee?
>>
>> If you want to appear smart, you better first read the other response I
>> wrote in this same thread many hours ago.
>> It contained a better solution. One that actually works and solves the OP
>> problem.
>
> oh, you mean this:
>
> /a[^b]*b/

Yes.

>
> still missing a ? after the * ... unless you want mixed results.

I have no clue what you are talking about.
What 'mixed results'?

I don't pretend to be a regex expert, so could you give me an example
please where the results differ?

So where gives
/a[^b]*b/
gives a different result than
/a[^b]*?/



>
> /a[^b]*?b/
>
> is appropriate. it may have worked with the test string, but not hardly a
> catch-all in the real world. since the op was confused about *?, your
> snippet pattern doesn't server to clear any of that up.
>
>
>> I fixed my own nonsense with a good solution that works in the other
>> response.
>> Will you do the same and fix the insulting crap you wrote about me in
>> here?
>
> no...but i did justify my comment further. ;^)
>

And you are definitely in a better mood than yesterday.
I saw you piss on more people yesterday in here....

Regards,
Erwin

Re: php regular expression doesn"t match

am 25.10.2007 11:07:19 von Erwin Moller

Erwin Moller wrote:
> Steve wrote:

> So where gives
> /a[^b]*b/
> gives a different result than
> /a[^b]*?/
>

should be:
Can you give an example where
/a[^b]*b/
differs from:
/a[^b]*?b/
of course.

Sloppy typing. Still the same coffee problem. ;-)

Erwin

Re: php regular expression doesn"t match

am 25.10.2007 16:18:15 von Steve

"Erwin Moller"
wrote in
message news:47205dcb$0$234$e4fe514c@news.xs4all.nl...
> Erwin Moller wrote:
>> Steve wrote:
>
>> So where gives
>> /a[^b]*b/
>> gives a different result than
>> /a[^b]*?/
>>
>
> should be:
> Can you give an example where
> /a[^b]*b/
> differs from:
> /a[^b]*?b/
> of course.
>
> Sloppy typing. Still the same coffee problem. ;-)

ahhh...the damned coffee. :)

it is interpreted differently in different engines. in preg, however, what
you think you're saying is NOT what you're saying.

'aabb' may be a string. your pattern should return three matches. 1) aab, 2)
ab and 3) aabb. this is because of greed inherent in your statement - which
is what the op wanted to know about anyway. using this:

/a[^b]*?b/

keeps the greed at bay. essentially, find an 'a' and any character until you
hit ONE 'b'. so, the above would have two matches...'aab' and 'ab'. it's all
about setting the marker in the preg engine. from that spot, the next set of
matching will begin. you're throwing yours down the street, when all you
needed to do was slide the mug down the bar counter.

no big deal? try it with preg_replace. ;^)

as for my mood? it's pretty consistent. okham's razor would have it that
more likely, two days ago, there were several people saying stupid things.
being consistent, i correct stupid things being said. but, you own your
perspective. see things how you will.

Re: php regular expression doesn"t match

am 26.10.2007 11:31:35 von Erwin Moller

Steve wrote:
> "Erwin Moller"
> wrote in
> message news:47205dcb$0$234$e4fe514c@news.xs4all.nl...
>> Erwin Moller wrote:
>>> Steve wrote:
>>> So where gives
>>> /a[^b]*b/
>>> gives a different result than
>>> /a[^b]*?/
>>>
>> should be:
>> Can you give an example where
>> /a[^b]*b/
>> differs from:
>> /a[^b]*?b/
>> of course.
>>
>> Sloppy typing. Still the same coffee problem. ;-)
>
> ahhh...the damned coffee. :)
>
> it is interpreted differently in different engines. in preg, however, what
> you think you're saying is NOT what you're saying.
>
> 'aabb' may be a string. your pattern should return three matches. 1) aab, 2)
> ab and 3) aabb. this is because of greed inherent in your statement - which
> is what the op wanted to know about anyway. using this:
>
> /a[^b]*?b/
>
> keeps the greed at bay. essentially, find an 'a' and any character until you
> hit ONE 'b'. so, the above would have two matches...'aab' and 'ab'. it's all
> about setting the marker in the preg engine. from that spot, the next set of
> matching will begin. you're throwing yours down the street, when all you
> needed to do was slide the mug down the bar counter.
>
> no big deal? try it with preg_replace. ;^)

Hi Steve,

Sorry, I hope you can keep this regex class running a little while
longer, because I still don't get it.
I DID try it in preg_replace, and got excactly the result I expected.

Lets look at your example: aabb
You talk about 3 matches, being:
1) aab
2) ab
3) aabb

I do not get that.
Look at the the following example: It uses my own homebrew version of
nongreedy *, being [^b]*b

$str="aabb";
$str=preg_replace("/a[^b]*b/", "peter", $str);
echo htmlentities($str);

That produces:
peterb

as exepted (by me at least), because * behaves nongreedy.


As far as I can tell, the feeded string starts matching as follows
(based on what I learned in 'Mastering Regular Expression' book):

- a matches right away
- next another a: matches with the non-b class [^b] (one time)
- Third character is a b
This DOESN'T match the non-b class, so the engine runs on the see if it
fits the next character, being a plain b in this example.
It matches (of course). So now we have a match (aab) and that gets
replaced by 'peter'.
- Then comes a 'b', that doesn't match the /a[^b]*b/
- end of string, and replacements.

So the result we have now is: peterb.

So I don't see your point about the 3 matches (aab,ab,aabb).

I can imagine my approach is somehow hugely ineficient maybe in other
cases/strings (I am not sure), but it does work OK.
Is that the case maybe? Efficiency?

As far as I can tell the /[^b]*b/ approach creates a nongreedy version
of /b*/

I must still miss something.
Could you give me an example where the results differ?
Or tell me WHAT I am missing?

As you can tell, I didn't finish my 'Mastering Regular Expression' book
yet. ;-)


>
> as for my mood? it's pretty consistent. okham's razor would have it that
> more likely, two days ago, there were several people saying stupid things.
> being consistent, i correct stupid things being said. but, you own your
> perspective. see things how you will.

That fine. I don't behave nicely on usenet either all the time. ;-)


Regards,
Erwin