Regex
Mudlet uses Perl Regular Expressions in a number of important ways. Regex is used to match alias commands or to match incoming lines with triggers. In triggers we can also capture this incoming data by surrounding the expressions in parentheses. Regex uses special characters to match text which is probably best shown through some examples.
Note: Regex is very powerful and goes beyond the basics shown on this page.
Note: Use a regex tester to test your code: regex101.com is highly recommended by Mudlet users.
Match a Digit
\d match a single digit \d+ match one or more digits
You get 5 gold. You get (\d) gold.
You get 150 gold. You get (\d+) gold.
Match an Alphanumeric Character
\w match a letter or number \w+ match one or more letters or numbers (e.g. a single word)
You see a dragon. You see (\w) dragon.
You see 1 dragon. You see (\w) dragon.
You see three dragons. You see (\w+) dragons.
Match a Letter Only
[a-z] match a single lower case only letter [A-Z] match a single upper case only letter [a-zA-Z] match an upper or lower case letter
You see a dragon. You see [a-z] dragon.
Will not match You see 1 dragon.
Match from List
(mage|warrior|cleric) match the word mage, warrior or cleric, but nothing else
Before you stands a mighty warrior. Before you stands a mighty mage. Before you stands a mighty cleric. Before you stands a mighty (mage|warrior|cleric).
Will not match Before you stands a mighty ogre.
Start and End of a Line
^ match the start of a line $ match the end of a line
^quest$
matches when a single word quest
is on its own line.
Match Everything
* match zero or more + match one or more ? optionally match
^(.*)$ match everything on a line, even if blank ^(.+)$ match everything on a line, but not a blank line
Examples
You see 30 gold. You see 1 silver.
^You see (\d+) (gold|silver).$
matches both lines. Capture group 1 would be 30 or 1. Capture group 2 would be gold or silver.
A warrior rests here. A mage stands here preparing to cast fireball.
^A (\w+) (rests|stands) here(.+)
matches both lines. Capture group 1 would be warrior
or mage
. Capture group 2 would be rests
or stands
. Capture group 3 would be .
or preparing to cast fireball.
Trigger Examples
Matching one unknown word
You can also set up a trigger to gather the weapons, gold or whatever skeletons could carry around with them. Since we do not know what the loot is exactly just yet, we will need to set up a trigger to match the line, identify the loot and take whatever it is that was dropped.
Examples for messages received could be:
The skeleton drops ring.
The skeleton drops gold. The skeleton drops scimitar. The skeleton drops wooden key.
"The skeleton drops " (including the last space character) is the generic segment of the line, but the loot itself varies. Thus, we need to tell the client to take whatever the skeleton dropped. We do this by setting up a so-called regular expression:
- In the data field titled "1" write the following perl regex type pattern:
^The skeleton drops (.+)\.$
- Make sure to change the dropdown menu for this line to "perl regex" as well
- In the big script box below write the following lua code:
send("take " .. matches[2])
The regular expression (.+) matches any characters that the client receives between "The skeleton drops " (NB: notice the blank/space character at the end) and the full-stop symbol that ends the sentence. Know that the variable matches[2] simply transfers the first matched text fitting the search criteria into the output. For this example, it will be the dropped loot we now will take automatically. This text may actually contain more than one word, like in the fourth example shown above.
In case you may wonder, matches[1] contains the entire line in whitch the matched text was found, whereas matches[2] contains only the first capture group. More on this in section two of the manual. The symbols ^ and $ indicate the start and end of a whole line.
Matching multiple unknowns
Now, we don't want to take only loot from skeletons but from many different sources.
Examples could be:
The skeleton drops ring.
The giant drops gold. The king drops scimitar. The box drops wooden key.
So let’s make a trigger that would gather the loot from anybody:
- In data field "1" write:
^(.+) drops (.+)\.$
- Select perl regex type pattern again
- Below write the lua code:
send("take " .. matches[3])
In this case, any time somebody, or something, "drops" something or someone else, the client will pick it up. Note that we used matches[3] instead of matches[2] this time, in order to pick up the second match. If we used matches[2], we’d end up picking up the skeleton’s corpse.
Matching known variants
If you’re playing a game in English, you’ll notice that these triggers probably won’t work due to English syntax. Compare:
The skeleton drops ring.
The skeleton drops a ring.
Chances are that you’ll see the later case a little more often. If we used our old regex, the trigger would produce something like this.
TRIGGERED LINE: The skeleton drops a ring.
OUR REACTION: take a ring
However most games can’t handle determiners in user-input, such as articles (i.e. a, an, the) or quantifiers (e.g. five, some, each). In effect, our triggered reaction won't suffice. Instead we would need to react with just "take ring" again.
To correctly handle lines like this, we could either create multiple triggers matching every possible article, which could become very cumbersome. Instead we make one regular expression filtering out all these words and phrases:
- Write a new perl regex type pattern:
(.+) drops (a|an|the|some|a couple of|a few|) (.+)\.$
- With this script:
send("take " .. matches[4])
Once again, note that this time we are using the third matched group through matches[4] now.
Basic Regex Characters
You already know (.+)
which will match to any and all characters that follow until the end of line or another specific text that you may put in your regex. How about if you only want to match to certain type of characters?
Retrieving numbers from triggers
Wildcards from triggers are stored in the matches[] table. The first wildcard goes into matches[2], second into matches[3], and so on, for however many wildcards do you have in your trigger.
For example, you’d like to say out loud how much gold did you pick up from a slain monster. The message that you get when you pick up the gold is the following:
You pick up 16 gold.
A trigger that matches this pattern could be:
- Perl Regex:
^You pick up (\d+) gold\.$
- Script:
echo("I got " .. tonumber(matches[2]) .. " gold!")
In your code, the variable matches[2] will contain the amount of gold you picked up - in this case, 16. Now you say out loud how much gold you did loot. Notice also that (\d+) will only recognize numbers but not letters or a space character.
Retrieving alphanumeric characters
Here’s a more advanced example by Heiko, which makes you talk like Yoda:
- Perl Regex:
^say (\w+) *(\w*) .*?(.*)
- Script:
send( "say "..matches[4].." "..matches[2].." "..matches[3] )
The trigger will recognize that you say something, and save in seperate groups the first word, the second word and then the rest of you text. Here the \w wildcards will match any numbers or letters but no non-alphanumeric characters. It then shows you say the rest of the text first, then the first word and finally the second word. Notice this will only affect the text displayed for yourself, but if you want to also adjust the text you are sending to other players, please see the chapter about aliases.
Helpful Links
- Mudlet manual: trigger pattern matching with regex
- Regex tutorial: https://regexone.com/
- Regex tester: https://regex101.com/