FutureQuest, Inc. FutureQuest, Inc. FutureQuest, Inc.

FutureQuest, Inc.
Go Back   FutureQuest Community > General Site Owner Support (All may read/respond) > PHP, Perl, Python and/or MySQL
User Name
Password  Lost PW

Reply
 
Thread Tools Search this Thread Display Modes
Old 11-21-1999, 03:42 PM   Postid: 43314
heath
Registered User

Forum Notability:
0 pts: Even-handed
[Post Feedback]
 
Join Date: Aug 1999
Posts: 233
Reg Expression is Greedy

How can I keep ereg_replace or preg_replace from being greedy?

Want the match to return first occurance of the regex, not the last.

In other words:

$str = "AA TAKE ME AWAY PPP AA XYZ PPP AA XYZ PPP jfkldjflkdjfsa PPP";

$str = ereg_replace ("AA.*PPP", "", $str);


I want to whack out "AA TAKE ME AWAY PPP" - not the whole string.  The only alternative I've been able to find is using {min,max} but this is imprecise because I never know how many characters are going to be in the other, unwanted matches.

Basically, I just want the first occurance found, not the greedy, biggest one that regex otherwise mandates.

Also - the above places a blank space in the string - how do I insert NOTHINGNESS?

Thanks in Advance
heath is offline   Reply With Quote
Old 11-21-1999, 03:47 PM   Postid: 43315
Justin
Visitor
 
Justin's Avatar

Forum Notability:
0 pts:
[Post Feedback]
 
Join Date: Jan 1999
Location: Kissimmee, FL
Posts: 3,672
$str = ereg_replace ("AA.*?PPP", "", $str);

That should do it, although it will replace all occurrances of AA...PPP in the string.

HTH

------------------
Justin Nelson
FutureQuest Support
Justin is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 visitors)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:13 PM.


Running on vBulletin®
Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.
Hosted & Administrated by FutureQuest, Inc.
Images & content copyright © 1998-2013 FutureQuest, Inc.
FutureQuest, Inc.