Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Tanuki
Novice


Joined: 06 Nov 2008
Posts: 38

PostPosted: Mon Nov 05, 2012 3:54 am   

Replace Multiple Stings with One Command
 
I want to get rid of the beginning A or AN or THE from a string. I can do this with 3 differant replace statements but I would like to do it in one. What would you do?

This is what I was thinking would work;
#echo %replace("a test",a|an|the, "")
returns: a test

Thank you
Tanuki
Reply with quote
Tanuki
Novice


Joined: 06 Nov 2008
Posts: 38

PostPosted: Mon Nov 05, 2012 4:00 am   
 
#echo %subregex("the Rat a High", "\b(?i)(?:a|the|of)\b","")
I found this and it will work but I would still like to know what I'm doing wrong above.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4691
Location: Pensacola, FL, USA

PostPosted: Mon Nov 05, 2012 4:14 am   
 
i would try to exclude the items from ever appearing in my string my using an anonymous variable in the trigger pattern

{a |an |the |} (*)

but for your questions i would do somthing like...

$temp=%replace("a test", " ", "|")
#IF (%ismember(%item($temp, 1), {a|an|the})) {#CALL %pop($temp)}
#SAY %replace($temp, "|", " ")


%replace expects everything it is passed to be a string, so the announymous variable array was an invalid datat type. you could use a #forall loop on a|an|the, but that would give you three prinouts
_________________
Discord: Shalimarwildcat
Reply with quote
Tanuki
Novice


Joined: 06 Nov 2008
Posts: 38

PostPosted: Mon Nov 05, 2012 4:23 am   
 
Thank you for the reply. In my current case i'm getting the variable via GMCP or I would take your first suggestion. But for now I will add that bit of code to my notes. I'm sure it will come in very handy someday and %pop comes in very handy right now Very Happy .

Thank You,
Tanuki
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Mon Nov 05, 2012 4:41 am   
 
%replace can only be used for simple replacements, one string with another. Your subregex should work fine, but if you wanted to do it with replace without nesting 3 of them, you could also use a loop like this:
Code:
$string = "a test"
#forall {a|an|the} {$string = %replace($string, %i, "")}
#echo $string


Keep in mind that both this replace method and your subregex method will remove the words from ANYWHERE in the string, not just the beginning. If you only want to remove them from the beginning, you'll want to add on a ^ to the start of your regex.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net