473,406 Members | 2,954 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,406 software developers and data experts.

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
Oct 12 '08 #1
3 1411
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.