MUDLET_REGEX
Regular Expressions, or RegEx for short, is a powerful pattern matching library. Mudlet uses
RegEx in Aliases, Triggers, and Scripts. For more in depth information about Regex please visit
some of the following websites:
Mudlet Trigger Engine: https://wiki.mudlet.org/w/Manual:Trigger_Engine
Mudlet Advanced Lua: https://wiki.mudlet.org/w/Manual:Advanced_Lua
These sites can help you test and debug your Regex when creating scripts. They also have some
helpful tutorials!
RegExr: https://regexr.com/
RegEx101: https://regex101.com/
Basic capturing tokens:
[abc] - Match a single character, a, b, or c
[^abc] - Match a character EXCEPT a, b, or c
[a-z] - Match a character in the range from a to z
[a-zA-Z] - Match a character in the range from a to z and A to Z
. - Match any single character
a|b - Match a OR b
\s - Match a whitespace character
\d - Match any digit
\w - Match any word
a? - Match 0 or 1 of a
a* - Match 0 or more of a
a+ - Match 1 or more of a
a{3} - Match exactly 3 of a
a{3,} - Match 3 or more of a
a{3,6} - Match between 3 and 6 of a
^ - Start of string
$ - End of string
For more, please refer to the above links!
Example RegEx patterns useful for Medievia:
(.*) is in (excellent) condition\.
This pattern will trigger on a target that is at full health.
The (.*) means capture everything, because names of people or mobs can contain many characters.
Parenthesis specify a capture group, a period captures any character, and an asterisk says capture
the preceding character zero to as many times as it can.
The (excellent) captures the word excellent, for use in a script so you know your target is full.
To access those captured values in a Lua script, use the matches table:
matches[2] will contain your target, and matches[3] will contain excellent in this case.
Note that the period at the end of the line is preceded with a backslash, this is because period
is a special RegEx character and needs to be escaped.
A more complicated pattern will capture the output of the stats command.
^\s*(?<hp>\d+)\/\s*(?<maxhp>\d+)hp\s*(?<mana>\d+)\/\s*(?<maxmana>\d+)m\s*(.*)mv\s*(-?[0-9]+)ac\s*(.*)
hr\s*(.*)dr\s*(-?\d+)a\s*(.*)xp
Here is a breakdown of the HP capturing at the beginning of the pattern:
The ^ character tells the RegEx engine to start capturing at the beginning of the line.
\s looks for a whitespace character, and the * after it means find as many as possible. Note that
there are no parenthesis around this as we do not care to have it as a capturing group for our
script!
(?<hp>\d+)\/\s*(?<maxhp>\d+)hp captures the current and max hp values, the ?<hp> is a named capture
group. Named groups help organize the captures for access in your script, allowing you to use
matches.hp instead of needing to figure out which capture number to use.
\d+ captures a number, with + meaning capture at least 1.
The parenthesis around this named capture group specify that we want to capture our HP.
To capture a forward slash, it needs to be escaped with a backslash as / is a special RegEx
character.
We then skip over any additional whitespace with \s*, and move onto capturing our max hp with
another named capturing group called maxhp, which will be available in matches.maxhp.
Finally the characters hp simply are matched as they are.
Feel free to Use RegExr or RegEx101 to paste the entire pattern and the output of your stats command
to get a more detailed breakdown of the entire pattern.
See also: MUDLET_TIPS
|