473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 (cm****@hotmail.com)

Oct 24 '07 #1
12 1934
cm****@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 (cm****@hotmail.com)
Regards,
Erwin Moller
Oct 24 '07 #2
Erwin Moller wrote:
cm****@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 (cm****@hotmail.com)

Regards,
Erwin Moller
Oct 24 '07 #3
cm****@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 (cm****@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
Oct 24 '07 #4
On Wed, 24 Oct 2007 08:50:48 +0200, <cm****@hotmail.comwrote:
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
Oct 24 '07 #5
On 24 Oct, 11:11, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Wed, 24 Oct 2007 08:50:48 +0200, <cmk...@hotmail.comwrote:
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
Mine was similar:
$strl = preg_replace('/(a.*b)*/','Peter',$strl);

Oct 24 '07 #6
On Tue, 23 Oct 2007 23:50:48 -0700, cm****@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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 24 '07 #7

"Erwin Moller"
<Si******************************************@spam yourself.comwrote in
message news:47*********************@news.xs4all.nl...
Erwin Moller wrote:
>cm****@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.
Oct 24 '07 #8
Steve wrote:
"Erwin Moller"
<Si******************************************@spam yourself.comwrote in
message news:47*********************@news.xs4all.nl...
>Erwin Moller wrote:
>>cm****@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
Oct 24 '07 #9

"Erwin Moller"
<Si******************************************@spam yourself.comwrote in
message news:47*********************@news.xs4all.nl...
Steve wrote:
>"Erwin Moller"
<Si******************************************@spa myourself.comwrote in
message news:47*********************@news.xs4all.nl...
>>Erwin Moller wrote:
cm****@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. ;^)
Oct 24 '07 #10
Steve wrote:

<snip>
>>
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

Oct 25 '07 #11
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
Oct 25 '07 #12

"Erwin Moller"
<Si******************************************@spam yourself.comwrote in
message news:47*********************@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.
Oct 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
3
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing...
2
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: LordHog | last post by:
Hello all, I am attempting to create a small scripting application to be used during testing. I extract the commands from the script file I was going to tokenize the each line as one of the...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.