php regular expression does not match
14 Message(s) by 6 Author(s) originally posted in php programming
| From: cmk128 |
Date: Wednesday, October 24, 2007
|
Hi
PHP's regular
expression look like does not
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@xxxxxxxxxxx)
| From: Erwin Moller |
Date: Wednesday, October 24, 2007
|
wrote in
message :
Hi
Hi,
PHP's regular expression look like does not 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 don't 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@xxxxxxxxxxx)
Regards,
Erwin Moller
| From: Erwin Moller |
Date: Wednesday, October 24, 2007
|
wrote in message:
wrote in message:
Hi
Hi,
PHP's regular expression look like does not 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 don't 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 does not solve the greedinessproblem.
Excuse me for the noise.
A better
solution 'd be to explode the
string first on space, and use
a
regex pr to modify if matched.
Regards,
Erwin Moller
thanks
from Peter (cmk128@xxxxxxxxxxx)
Regards,
Erwin Moller
| From: Erwin Moller |
Date: Wednesday, October 24, 2007
|
wrote in message:
Hi
PHP's regular expression look like does not 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@xxxxxxxxxxx)
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
| From: Rik Wasmus |
Date: Wednesday, October 24, 2007
|
wrote in message:
Hi
PHP's regular expression look like does not 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_*
function s
<?php
$str=3D"a1b a3b";
echo preg_replace("/a.*?b/", "peter", $str);
?>
-- =
Rik Wasmus
| From: Captain Paralytic |
Date: Wednesday, October 24, 2007
|
wrote in message:
wrote in message:
> Hi
> PHP's regular expression look like does not 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
Mine was similar:
$strl = preg_replace('/(a.*b)*/','Peter',$strl);
| From: Andy Hassall |
Date: Wednesday, October 24, 2007
|
wrote in message:
PHP's regular expression look like does not 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@xxxxxxxxxxx ::
http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space ::
disk and
FTP usage analysis
tool
| From: Steve |
Date: Wednesday, October 24, 2007
|
"Erwin Moller"
wrote in message in
message
wrote in message:
wrote in message:
Hi
Hi,
PHP's regular expression look like does not 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 don't 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 does not solve the greedinessproblem.
Excuse me for the noise.
A better solution'd 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 will be able to make such leaps...and be a
bit more accurate. your 'better
solution' is tripe. as others have
point ed 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.
| From: Erwin Moller |
Date: Wednesday, October 24, 2007
|
wrote in message:
"Erwin Moller"
wrote in message in
message
wrote in message:
wrote in message:
Hi
Hi,
PHP's regular expression look like does not 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 don't 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 does not solve the greedinessproblem.
Excuse me for the noise.
A better solution'd 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 will 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 message 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.
wrote in message about me in here?
Erwin Moller
| From: Steve |
Date: Wednesday, October 24, 2007
|
"Erwin Moller"
wrote in message in
message
wrote in message:
"Erwin Moller"
wrote in message in
message
wrote in message:
wrote in message:
Hi
Hi,
PHP's regular expression look like does not 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 don't 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 does not solve the greedinessproblem.
Excuse me for the noise.
A better solution'd 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 will 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 message 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 does not
server to clear any of that up.> I fixed my own nonsense with a good solution that works in the other
response.
wrote in message about me in
here?
no...but I did justify my
comment further. ;^)
| From: Erwin Moller |
Date: Thursday, October 25, 2007
|
wrote in message:
<snip>
Bullshit? More pots of coffee?
If you want to appear smart, you better first read the other response I
wrote in message 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
talk ing about.
What 'mixed results'?
I do not 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 does not server to clear any of that up.
I fixed my own nonsense with a good solution that works in the other
response.
wrote in message 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
| From: Erwin Moller |
Date: Thursday, October 25, 2007
|
wrote in message:
wrote in message:
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
| From: Steve |
Date: Thursday, October 25, 2007
|
"Erwin Moller"
wrote in message in
message
wrote in message:
wrote in message:
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
engine s. in preg, however, what
you think you're saying isn't 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've 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've 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'll.
| From: Erwin Moller |
Date: Friday, October 26, 2007
|
wrote in message:
"Erwin Moller"
wrote in message in
message
wrote in message:
wrote in message:
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 isn't 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've 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 do not 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 don't 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 DOES not 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've a match (aab) and that gets
replaced by 'peter'.
- Then comes a 'b', that does not match the /a[^b]*b/
- end of string, and replacements.
So the result we've now is: peterb.
So I do not 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 did not finish my 'Mastering Regular Expression' book
yet. ;-)>
as for my mood? it's pretty consistent. okham's razor would've 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'll.
That fine. I do not behave nicely on usenet either all the time. ;-)Regards,
Erwin
Next Message: .htaccess and mod_rewrite