A regular expression, regex or regexp for short, is a special text string used to describe a search pattern. These are used to check to see if another text string matches a specific pattern you're looking for.
You may be familiar with pattern searching using wildcards, such as: *.txt.
You may also be familiar with pattern searching using triggers in MUD clients.
For example: You might use the following syntax: %* tells the group, '%s'.
This is an example of a simplified regex language often offered by clients to make it easier for the user to use.
Regular expressions works similarly to the aforementioned pattern matching languages, but allows you to do much more. This comes at a cost, as regex is also a little more complex to learn. I will do my best in this tutorial to show you how easy regex can be, and give you examples of triggers you would use for LegendMUD using these regexes.
and non-printable characters
A regex is made up of a string of text characters. In many cases, a regex will include simple text to match against, such as:tells the group. This will search the input text for that string of text specifically. We want to do more than simply search for literal text, so we need to learn about the special characters and their uses.
The following characters are considered special characters within a regex, and will give the pattern string special effects:
[, ], (, ), {, }, *, +, ?, |, ^, $, ., \, /
The following sections will go into more detail about each of these special characters, but for now, note that each of these characters can not be used to match against themselves in an input string. If you want to match against, say, a left square bracket ([) you would need to escape the special character in the pattern string. Escaping can be accomplished using the backslash character (\). The pattern string would then look like this: \[.
The character class is the most basic form of using regex special characters to create a pattern string. This tells the regex engine to match only one out of several characters.
Let's say you want to create a set of triggers that colors every color word that shows up in Legend (the word 'red' will appear in the color red, 'yellow' in yellow, etc.). This is pretty easy, as you can create a trigger for each color, and then just replace the color of the word in the script after it matches. But what about the color grey? In some countries, it's spelled grey, but in others, it's spelled gray. Using a character class, you can match against either e or a using [ae] (or [ea], order doesn't matter). The pattern gr[ae]y will look for gr, then only one of either a or e, and finally, y.
Character classes can do much more than just the alphabet, however. It can allow numbers and other characters. It can also accept ranges for characters. The following pattern will match against any alphanumeric characters: [A-Z0-9a-z] (again, order can be played with, [a-zA-Z0-9] is also valid).
Character classes can also be defined as "anything but" classes using the ^ special character. [^a] will match against any character as long as it is not a.
This works well for single characters, but if you want to match against a series of characters, you'll need to use the repeating operators: ?, *, or +. ? will match 0 or 1 characters, + will match 1 or more characters, and * will match 0 or more characters. The following pattern will match against any word that contains only characters in the alphabet: [A-Za-z]+.
As you've probably guessed by now, some character classes will be used a lot. Luckily, there are some shorthand notations to make this a bit easier. The following table will list out each shorthand as well as what it translates into if you were to write out the character class yourself.
| Shorthand | Meaning | Translation | Notes |
|---|---|---|---|
\d |
digit | [0-9] |
|
\w |
word | [A-Za-z0-9_] |
|
\s |
whitespace | [ \t\r\n\f] |
A space, tab, carriage return, line feed, or form feed |
To show you an example, let's say you wanted to match against the HP in your prompt: HP: 340/550. You could use the following regex pattern to match: HP: \d+\/\d+. Notice the \/, since / is a special character, it needs to be escaped using the \ character.
There are also negated shorthands, by simply capitalizing the character in the shorthand, you will look for "anything but". This means that \D translates to [^0-9], \W translates to [^A-Za-z0-9_], and \S translates to [^ \t\r\n\f].