Regex

From Mudlet
Revision as of 06:27, 16 July 2024 by Zooka (talk | contribs) (initial creation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.


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.

Cheat Sheet

Helpful Links