Difference between revisions of "Regex"
Line 1: | Line 1: | ||
Mudlet uses Perl '''Reg'''ular '''Ex'''pressions in a number of important ways. Regex is used to match alias commands or to match incoming lines with triggers. In triggers we capture this data by surrounding the expressions in parentheses. Regex uses special characters to match text which is probably best shown through some examples. | Mudlet uses Perl '''Reg'''ular '''Ex'''pressions in a number of important ways. Regex is used to match alias commands or to match incoming lines with triggers. In triggers we capture this 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}} 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. | + | |
+ | {{note}} Use a regex tester to test your code: [http://regex101.com regex101.com] is highly recommended by Mudlet users. | ||
+ | |||
== Match a Digit == | == Match a Digit == | ||
Line 68: | Line 71: | ||
^(.*)$ match everything on a line, even if blank | ^(.*)$ match everything on a line, even if blank | ||
^(.+)$ match everything on a line, but not a blank line | ^(.+)$ match everything on a line, but not a blank line | ||
+ | |||
+ | == Combined 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 <code>warrior</code> or <code>mage</code>. Capture group 2 would be <code>rests</code> or <code>stands</code>. Capture group 3 would be <code>.</code> or <code>preparing to cast fireball.</code> | ||
== Helpful Links == | == Helpful Links == |
Revision as of 07:15, 16 July 2024
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 capture this 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
Combined 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.
Helpful Links
- Regex tutorial: https://regexone.com/
- Regex tester: https://regex101.com/