By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

regexp

P: n/a
hi folks.

I'm in trouble
PHP on apache:

$such =array('/a*d/');
$ersetz=array(' ersetz ');
echo "before!!!! $filestream"; // -->
abcdefghi abcdefghi
$filestream=preg_replace($such, $ersetz, $filestream);
echo "after !!!! $filestream"; // --abc
ersetz efghi abc ersetz efghi

but I'm expecting:
ersetzefghi ersetzefghi

where's my error?
tia
walter
Oct 12 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Walter Kerelitsch wrote:
hi folks.

I'm in trouble
PHP on apache:

$such =array('/a*d/');
$ersetz=array(' ersetz ');
echo "before!!!! $filestream"; // --
abcdefghi abcdefghi
$filestream=preg_replace($such, $ersetz, $filestream);
echo "after !!!! $filestream"; // --abc
ersetz efghi abc ersetz efghi

but I'm expecting:
ersetzefghi ersetzefghi

where's my error?
tia
walter
Hello Walter,

The asterisk (*), in regular expressions, is not the wildcard character you are
trying to use. It indicates that the preceding character can be repeated any
number of times (even 0 times).

So /a*d/ will match these: "d", "ad", "aad", "aaad", etc. In your case, it
matched and replaced only the "d", leading to what you see.

What you are trying to do requires the regular expression /a.*?d/. The period
(.) matches any single character, like "abd" or "acd". ".*" means any number of
unspecified characters, like "abcd" or "abcdefghi abcd".

The question mark means that it should try to match as few characters as
possible ("non-greedy" search). /a.*d/ may replace "abcdefghi abcd" all at once,
which you don't want - /a.*?d/ makes sure that the matching stops at the first
"d" that is encountered.

--Christoph

PS: If you expect "ersetzefghi", then of course the replacement string has to be
' ersetz', not ' ersetz ' like it is now. :)

--
"Omniscient? No, not I; but well-informed."
----------------------
XMPP: ch****************@gmail.com
AOL: 313125838 / cburschka
Key: http://pgp.mit.edu:11371/pks/lookup?...rch=0x55A52A2A
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI8xqJhbef5VWlKioRAl00AJ9vdgdBW4ktyl4fiSuFyo hqho1q5gCgpKph
exkh8RXm6E4BsQEZ9mD+vXI=
=Hb8E
-----END PGP SIGNATURE-----

Oct 13 '08 #2

P: n/a
Christoph Burschka wrote:
Walter Kerelitsch wrote:
>hi folks.

I'm in trouble
PHP on apache:

$such =array('/a*d/');
$ersetz=array(' ersetz ');
echo "before!!!! $filestream"; // -->
abcdefghi abcdefghi
$filestream=preg_replace($such, $ersetz, $filestream);
echo "after !!!! $filestream"; // --abc
ersetz efghi abc ersetz efghi

but I'm expecting:
ersetzefghi ersetzefghi

where's my error?
tia
walter


Hello Walter,

The asterisk (*), in regular expressions, is not the wildcard character you are
trying to use. It indicates that the preceding character can be repeated any
number of times (even 0 times).

So /a*d/ will match these: "d", "ad", "aad", "aaad", etc. In your case, it
matched and replaced only the "d", leading to what you see.

What you are trying to do requires the regular expression /a.*?d/. The period
(.) matches any single character, like "abd" or "acd". ".*" means any number of
unspecified characters, like "abcd" or "abcdefghi abcd".

The question mark means that it should try to match as few characters as
possible ("non-greedy" search). /a.*d/ may replace "abcdefghi abcd" all at once,
which you don't want - /a.*?d/ makes sure that the matching stops at the first
"d" that is encountered.

--Christoph

PS: If you expect "ersetzefghi", then of course the replacement string has to be
' ersetz', not ' ersetz ' like it is now. :)
Also, you don't need the arrays for this. peg_replace will replace all
instances of regex.

M. Turner
Oct 13 '08 #3

P: n/a
thanks a lot, christoph and myron!

I had a complete misunderstanding of regexp, thought that simple
substitutions are also simple here...

I'm not really firm witth regex, so I will visit this group later again...

@Myron: I want to expand this little proggy to make *a lot of* replacements,
so I began with an array...

greetings walter


"Walter Kerelitsch" <je****@microisoft.comschrieb im Newsbeitrag
news:65**************************@news.chello.at.. .
hi folks.

I'm in trouble
PHP on apache:

$such =array('/a*d/');
$ersetz=array(' ersetz ');
echo "before!!!! $filestream"; // -->
abcdefghi abcdefghi
$filestream=preg_replace($such, $ersetz, $filestream);
echo "after !!!! $filestream"; // --abc
ersetz efghi abc ersetz efghi

but I'm expecting:
ersetzefghi ersetzefghi

where's my error?
tia
walter

Oct 13 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.