Regex for words in a command

by Razenth

Back to Mechanic's Corner.

Razenth2010-01-24 06:38:45
I want to do something where in a command, it will replace all instances of a certain pattern with another. For example, if I type in the command 'cat cat cat cat snake dog badger cat', and I want it to replace cat with badger, the client will send 'badger badger badger badger snake dog badger badger" Anyone know of a way to do this, or what characters I should be using?


EDIT: Could someone close the 20 threads below this? I have no idea how it happened...
Esano2010-01-24 07:28:31
A) Tell us what you're using (e.g. Mushclient)

cool.gif If you ARE using Mushclient, I'd make an alias to just match ^(.*?)cat(.*?)$

You'd need to make it not match again, as otherwise you'd probably send it several times.

Something like the following script would then sub badger for cat and send the result ....

Send(string.gsub("%0","cat","badger"))
Eldanien2010-01-24 07:33:13
I'd need more input... how is it determined which text will be replaced with which other text?
Razenth2010-01-24 07:34:09
Mush, Lua.

I want to match like, every word with an h as the first letter, or every word that ends in 'er'. And replace it without something else.
Ssaliss2010-01-24 07:53:34
The regex for matching that would be (+) and (+er), respectively. Regexes don't replace though, you'd need to use another coding language to do that. Also, I assumed that you'd want it to match both upper- and lowercase h's in the beginning of words.
Eldanien2010-01-24 09:39:01
I put together this alias so you'd have something to look at for comparison. It's rather pointless and goes out of the way to do things the long way, but shows off some of what you might be looking for. Or it might be looking at your question from the entirely wrong perspective.

To play around with this in your client, copy the text in the codebox, go to your aliases window in MUSH (Shift+Ctrl+9) and click the Paste button. That will add the alias in. The script will expect a MUSH variable named Do_I_Convert_Mantras.

example of alias in use:
input:
mantra chant lp na ku

sends to the server:
mantra chant lumphet na krakuti

CODE
      
                   match="^mantra chant (.*?) (.*?) (.*?)$"
          enabled="y"
          regexp="y"
          send_to="12"
          ignore_case="y"
          sequence="100"
         >
         local firstmantra = "%1"
       local secondmantra = "%2"
       local thirdmantra = "%3"
      
       local fullmantra = firstmantra .. " " .. secondmantra .. " " .. thirdmantra
       local ConvertMantra = world.GetVariable ("Do_I_Convert_Mantras")
      
       if string.lower (ConvertMantra) == "yes" then
         fullmantra = string.gsub (fullmantra, "na", "00")  -- get rid of 'na' for consideration
      
         fullmantra = string.gsub (fullmantra, "am", "angmun")
         fullmantra = string.gsub (fullmantra, "ap", "angphet")
         fullmantra = string.gsub (fullmantra, "au", "anguti")
         fullmantra = string.gsub (fullmantra, "cm", "chumun")
         fullmantra = string.gsub (fullmantra, "cp", "chuphet")
         fullmantra = string.gsub (fullmantra, "cu", "chuuti")
         fullmantra = string.gsub (fullmantra, "km", "krakmun")
         fullmantra = string.gsub (fullmantra, "kp", "krakphet")
         fullmantra = string.gsub (fullmantra, "ku", "krakuti")
         fullmantra = string.gsub (fullmantra, "lm", "lummun")
         fullmantra = string.gsub (fullmantra, "lp", "lumphet")
         fullmantra = string.gsub (fullmantra, "lu", "lumuti")
         fullmantra = string.gsub (fullmantra, "om", "ooshmun")
         fullmantra = string.gsub (fullmantra, "op", "ooshphet")
         fullmantra = string.gsub (fullmantra, "ou", "ooshuti")
      
         fullmantra = string.gsub (fullmantra, "00", "na")  -- put na back in
       end
      
       world.Send ("mantra chant " .. fullmantra)

        
     Â