Regex

From Mudlet
Revision as of 06:35, 16 July 2024 by Zooka (talk | contribs)
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.

Note Note: Regex is very powerful and goes beyond the basics shown on this page. Note 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

Helpful Links