Support | General

General – Regular Expression (RegEx)

Regular expressions are a way to make your bot listen intelligently for what you’d like to respond to. Regular expressions are utilized by listeners, even if they are created the “basic” way. Let’s take a closer look at how to use regular expressions.

An example of how to use regex

When editing an intent, you have the option of using the Listener Builder (simple keywords or phrases you want the bot to detect) or using RegEx in the Advanced Listener. To use the Advanced Listener, simply toggle the button beside “Advanced Listener?” to open up the builder. 

You can test your Regex in Ochatbot or learn more and test here https://regex101.com/

Basic RegEx

Here are some of the most basic and useful regular expressions and examples:

| – separates lists of words to be matched (“cat|dog” will match a phrase containing either “cat” or “dog”)

^ – match the start of a user’s input (“^cat” will match anything starting with “cat”)

$ – match the very end of a user’s input (“cat$” will match any sentence ending with “cat”)

\b – match a word break (“\bcat\b” will match “cat” exactly, but not “cats” or “vacation”)

\s – a literal space between words (“cat\sand\sdog”)

(?=.*cat)(?=.*dog).* listens for a phrase containing two words (will match any phrase with the words “cat” and “dog” somewhere in it)

(?=.*(won’t|can’t|cant|wont|taking|will not))(?=.*order).* This will listen for  sentences like:

“It won’t place my order”
“It wont let me order
“My orders won’t go through”

(?=.*(won(‘)?t|can(‘)?t|taking|will not))(?=.*order).* is another option the (‘)? is saying that the apostrophe is optional

More Advanced RegEx General rules

A “.” in a regular expression will match any single character.  So, if you want to specifically match a “.” character, you need a “\” before it — so, use “\.” For example, a regular expression: “domain.com” would also match domianxcom.  If you want the “.” matched exactly, the regular expression would be: “domain\.com”

Same is with a “/” you should have a \ before it like \/ but in most cases you will be OK with out it.

The “.*” allows you to create a wild card

The \s is a literal space between two words.

 

The following characters are the meta characters that give special meaning to the regular expression search syntax:

\ the backslash escape character.

The backslash gives special meaning to the character following it. For example, the combination “\n” stands for the newline, one of the control characters. The combination “\w” stands for a “word” character, one of the convenience escape sequences while “\1” is one of the substitution special characters.

Example: The regex “aa\n” tries to match two consecutive “a”s at the end of a line, inclusive the newline character itself.

Example: “a\+” matches “a+” and not a series of one or “a”s.

^ the caret is the start of line anchor or the negate symbol.

Example: “^a” matches “a” at the start of a line.

Example: “[^0-9]” matches any non digit.

$ the dollar is the end of line anchor.

Example: “b$” matches a “b” at the end of a line.

Example: “^b$” matches the empty line.

{ } the open and close curly bracket are used as range quantifiers.

Example: “a{2,3}” matches “aa” or “aaa”.

[ ] the open and close square bracket define a character class to match a single character.

The “^” as the first character following the “[” negates and the match is for the characters not listed. The “-” denotes a range of characters. Inside a “[ ]” character class construction most special characters are interpreted as ordinary characters.

Example: “[d-f]” is the same as “[def]” and matches “d”, “e” or “f”.

Example: “[a-z]” matches any lowercase characters in the alfabet.

Example: “[^0-9]” matches any character that is not a digit.

Example: A search for “[][()?<>.*?]” in the string “[]()?<>.*?” followed by a replace string “r” has the result “rrrrrrrrrrrrr”. Here the search string is one character class and all the meta characters are interpreted as ordinary characters without the need to escape them.

( ) the open and close parenthesis are used for grouping characters (or other regex).

The groups can be referenced in both the search and the substitution phase. There also exist some special constructs with parenthesis.

Example: “(ab)\1” matches “abab”.

. the dot matches any character except the newline.

Example: “.a” matches two consecutive characters where the last one is “a”.

Example: “.*\.txt$” matches all strings that end in “.txt”.

* the star is the match-zero-or-more quantifier.

Example: “^.*$” matches an entire line.

+ the plus is the match-one-or-more quantifier.

? the question mark is the match-zero-or-one quantifier. The question mark is also used in special constructs with parenthesis and in changing match behaviour.

| the vertical pipe separates a series of alternatives.

Example: “(a|b|c)a” matches “aa” or “ba” or “ca”.

< > the smaller and greater signs are anchors that specify a left or right word boundary.

– the minus indicates a range in a character class (when it is not at the first position after the “[” opening bracket or the last position before the “]” closing bracket.

Example: “[A-Z]” matches any uppercase character.

Example: “[A-Z-]” or “[-A-Z]” match any uppercase character or “-“.

& the and is the “substitute complete match” symbol.

Frequently Asked Questions

I’m still a little overwhelmed by all of this so far. Can you help?

That’s very understandable if you are new or unfamiliar with any type of coding. Try visiting https://www.w3schools.com/ and search “regular expression” in the search bar for more detailed information. If you are still having trouble, contact Support and we will assist you the best we can.

We Are Here to Help

Here are the different ways you can contact support:

  • When logged in, click the "Contact Support" button. Your information will be autopopulated so just type in your question or comment.
  • To create a ticket when not logged in email: [email protected]
  • Call us during business hours: 8 a.m.-5 p.m. MST 800-700-8077.
  • If outside the US: send an email to [email protected] and we will set up a meeting over Zoom.