Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 12th, 2008, 11:15 PM
Walter Kerelitsch
Guest
 
Posts: n/a
Default regexp

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


  #2  
Old October 13th, 2008, 10:55 AM
Christoph Burschka
Guest
 
Posts: n/a
Default Re: regexp

Walter Kerelitsch wrote:
Quote:
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: christoph.burschka@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-----

  #3  
Old October 13th, 2008, 07:15 PM
Myron Turner
Guest
 
Posts: n/a
Default Re: regexp

Christoph Burschka wrote:
Quote:
Walter Kerelitsch wrote:
Quote:
>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
  #4  
Old October 13th, 2008, 10:15 PM
Walter Kerelitsch
Guest
 
Posts: n/a
Default Re: regexp

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" <jemand@microisoft.comschrieb im Newsbeitrag
news:6577$48f27654$5472d2de$23409@news.chello.at.. .
Quote:
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
>

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles