Skip to content

Regular expressions

jeremyredhead edited this page Jan 18, 2021 · 5 revisions

What's a regular expression? Think of it as pattern matching on steroids.

BotBot uses regular expressions (a.k.a. regexes) so that bot developers can use powerful pattern matching functionality without needing to create their own dedicated bots.

BotBot's regexes are case-insensitive. This means that, for example, a regex foo will also match the string FOO.

Basic tokens

Here are some basic tokens you can use in your regexes.

  • .: Matches any character except a newline.
  • ?: Makes the previous token optional.
  • *: Matches the previous token zero or more times.
  • +: Matches the previous token one or more times.
  • (red|green|blue): Matches any of the options separated by the pipe character |; in this case, the options are "red", "green", and "blue".
  • [abc]: Matches one of the characters between the brackets []; in this case, it will match one of "a", "b", or "c".
  • [^abc]: Matches any character not between the brackets [] (excluding the caret ^); in this case, it will match any character but "a", "b", or "c".
  • ^: Matches the beginning of a message.
  • $: Matches the end of a message.
  • \: If followed by a special character, causes that character to be treated as a plaintext token.

More info

A comprehensive tutorial and reference at regular-expressions.info.

The developers of BotBot are not affiliated with regular-expressions.info in any way.

Additionally, the Python regex documentation should provide the most accurate reference for the regex engine used in BotBot.

Technical details

BotBot uses the Python regex engine for regex parsing and matching.

BotBot regexes use only the flag re.IGNORECASE.
BotBot regexes do not use the flag re.UNICODE because this flag is redundant in Python 3, as matches are Unicode by default (see the documentation).

Clone this wiki locally