basic regular expression in an alias

Posted by MushclientNewbie on Mon 29 Nov 2004 10:06 PM — 7 posts, 33,454 views.

USA #0
My problem: I use aliases for long mud command inputs. Like, I imagine, most everyone does. Since I had started using MC so long ago I've fixed my problem in a sort of round about-way. I've been making two triggers for every single alias I use. For example:

<alias
 match="ca *"
 enabled="y"
 sequence="100"
>
<send>commune 'armor' %1</send>
</alias>
<alias
 match="ca"
 enabled="y"
 sequence="100"
>
<send>commune 'armor'</send>
</alias>


This way I could use the first alias 'ca *' to give armor to someone else while I could use the first alias 'ca' to give armor to myself quickly since the mud reads spells cast with no target as self-cast (offensive spells with no target are directed at your current combat target - which is nice when blinded since givning it a target just returns that the person can't be found).

It's worked fine up until now, but by now I'm awful tired of having my alias screen cluttered up with repeat alias after repeat alias. For now I've worked out this half-baked solution that looks like:

<alias
 match="ca*"
 enabled="y"
 sequence="100"
>
<send>commune 'armor'%1</send>
</alias>


Which works fine until I enter something that starts with ca... like 'cackle.' I know regular expressions can take care of this. I've read through the helpfile on it and so far what I have (not working) is this:

<alias
 match="^ca(| \*)"
 enabled="y"
 regexp="y"
 sequence="100"
>
<send>commune 'armor' %1</send>
</alias>


No matter what I type or how hard I type it <^_^< all I ever get returned is "commune 'armor' " with that little space after it. Am I using the | operator wrong? Or is it just that it won't give me a 'match on nothing' choice?
Amended on Mon 29 Nov 2004 10:07 PM by MushclientNewbie
USA #1
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4695

Scroll down to the last post by nick.
Basically instead of using a switch (pipe, whatever), you use a question mark. It's all explained in that thread.
USA #2
Thanks Flannel. It works:

<alias
 match="^ca( .*)?$"
 enabled="y"
 regexp="y"
 send_to="12"
 sequence="100"
>
<send>if "%1" = "" then
  Send "commune 'armor'"
 else
  Send "commune 'armor'%1"
end if</send>
</alias>


But why? When I try and break it down this is what I get...

^ is easy. That's there to make sure it matches at the beginning of a string so I don't fire it accidentally on something like, "He's in the can." Can doesn't match 'ca' but its the thought here that counts, right?

() matches any pattern inside. Here the pattern is " .*" space-period-asteric. Here is where I get a little confused. The period tells the mud that this expression matches on a single character and the asteric goes on further to say that it matches anything 0 times or more, right? So the insides of the pattern say that it matches anything with 0 or more spaces? Why didn't it need the \ operator to tell it that it's for a wildcard?

? then this says that all the previous stuff in the pattern can match once or not-at-all?

$ follows it all up to match on the not-at-all case for the 'ca' with nothing after it which runs the if "%1" = "" case.
Amended on Mon 29 Nov 2004 11:19 PM by MushclientNewbie
USA #3
the slash is only for special things, such as \n (for a newline) \d (for a digit) and such. a . is "anything" and then the * is a quantifier.
so, '.*' matches on ANYTHING.

and then we need a space in there, since otherwise we would match on 'cackle'. And the $ at the end is just like the ^ at the beginning, it makes sure we match the whole line, and dont skip anything important.

a backslash sequence is a more specific type of '.' it matches on 'any' of a specific type of thing.

#4
so (.+) would be multiple quantifiers?
Amended on Mon 13 Dec 2004 11:04 AM by David Berthiaume
USA #5
Actually. + and * are nearly the same. They both mean "X or more of the character or group I follow." The difference is that * means "0 or more", while + means "1 or more". So basically, if you had two lines:

The duck.
The mad duck.

Then "The .* duck\." would match both lines, while "The .+ duck\." would only match the second line. BTW, I meantioned 'group' above. That means a trigger like "The (mad |sad |dead )*duck\.", which would match:

The mad duck.
The mad mad mad duck.
The mad sad dead mad duck.

Or any other combination, since you are asking it not to just look for those specific words, but any number of them. Note also the location of the spaces and '*duck'. If you think about it, the reason is that "The (mad|sad|dead)* duck\." would imply a line like 'The madsaddead duck.', which obviously won't work, so the spaces have to be defined in the groups themselves.
Australia Forum Administrator #6
Plus, you can be more specific, like I did in the other recent post about your skills. Something like:


The (mad ){2,4}duck


This matches between exactly 2 and 4 of "mad " so it would match only

The mad mad duck
The mad mad mad duck
The mad mad mad mad duck