Regular Expressions



Introduction

Regular Expressions are a very powerful and very complex method for describing matches for a pattern of text. Regular expressions - also known as regexes, grep expressions or grep patterns - use a set of special characters to describe a template that can match a variety of text patterns. This lets you do "wildcard" matches that can, say, set a trigger that will work on all text paged from you or to you, by looking for the common bits in the responses from your muck's page function.

A full overview of regular expressions is beyond what I can do here, but here's a basic list of the main building blocks, and some examples.

Basic regular expression building blocks:

Return to Top

Repetitions

Matching a single character can sometimes be useful, but matching a repeated series of characters is more so.

Return to Top

Substituting text, a.k.a. backreferences

Cantrip triggers allow you to substitute your own text for the incoming text that tripped the trigger. By using parentheses to group parts of your regular expression into subexpressions, you can use them as part of your replacement text; use a double percent sign (%%) plus the number of the subexpression you want to use in your substituted text. This is called a backreference. For example:

Take the following regular expression example:

^.*> .*$

...which matches an entire line of text from a puppet. (The first .* catches the characters between the start of the line and the > that marks the end of the puppet's name; the second .* catches the rest of the text to the end of the line.) If you then break up the regex into subexpressions with parentheses, as follows:

(^)(.*)(> )(.*)($)

you can use %%2 to backreference the second subexpression in the expression - the name of the puppet - in your substituted text. %%4 will give you the text the puppet received. So for the following line:

Frito> Akemi says, "That would be telling."

using this regular expression for your substituted text:

Your puppet %%2 reported, -= %%4 =-

will give you

Your puppet Frito reported, -= Akemi says, "That would be telling." =-

There are far more things you can do with regular expressions, but this should be enough to cover most uses in Cantrip.

Return to Top

Some regular expression examples:

Return to Top

Places in Cantrip where regular expressions are used:

Return to Top

Other information sources for regular expressions:

(Note that not all of these references work with the specific regular expression dialect that Apple's regex library uses. I am still looking for documentation on Apple's dialect, and will list the differences below when I find them.)

Differences in regular expression dialects, as seen in Cantrip

Return to Top