php regular expression doesn't match | | |
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 cmk128@hotmail.com wrote: Hi, Quote:
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. Quote:
$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); Quote:
thanks
from Peter (cmk128@hotmail.com)
>
Regards,
Erwin Moller | | | | re: php regular expression doesn't match
Erwin Moller wrote: Quote: cmk128@hotmail.com wrote: >
Hi,
> Quote:
> 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.
> Quote:
>$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 Quote:
> Quote:
>thanks
>from Peter (cmk128@hotmail.com)
>>
>
Regards,
Erwin Moller
| | | | re: php regular expression doesn't match cmk128@hotmail.com wrote: Quote:
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
On Wed, 24 Oct 2007 08:50:48 +0200, <cmk128@hotmail.comwrote: Quote:
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
<?php
$str="a1b a3b";
echo preg_replace("/a.*?b/", "peter", $str);
?>
--
Rik Wasmus | | | | re: php regular expression doesn't match
On 24 Oct, 11:11, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote: Quote:
On Wed, 24 Oct 2007 08:50:48 +0200, <cmk...@hotmail.comwrote: Quote:
Hi
PHP's regular expression look like doesn't support .*? syntax. So i
cannot match the shortest match. For exmaple:
> Quote:
$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
>
<?php
$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
On Tue, 23 Oct 2007 23:50:48 -0700, cmk128@hotmail.com wrote: Quote:
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
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrote in
message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl... Quote:
Erwin Moller wrote: Quote:
> cmk128@hotmail.com wrote: >>
>Hi,
>> Quote:
>> 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.
>> Quote:
>>$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
Steve wrote: Quote:
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrote in
message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl... Quote:
>Erwin Moller wrote: Quote:
>> 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 Quote:
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
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrote in
message news:471f52a9$0$241$e4fe514c@news.xs4all.nl... Quote:
Steve wrote: Quote:
>"Erwin Moller"
><Since_humans_read_this_I_am_spammed_too_much@spa myourself.comwrote in
>message news:471f0bf1$0$226$e4fe514c@news.xs4all.nl... Quote:
>>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 Quote:
>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. Quote:
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
Steve wrote:
<snip> Quote: Quote:
>>
>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. Quote:
>
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]*?/ Quote:
>
/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.
>
> Quote:
>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
Erwin Moller wrote: Quote:
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
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrote in
message news:47205dcb$0$234$e4fe514c@news.xs4all.nl... Quote:
Erwin Moller wrote: > Quote:
>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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|