Regular Expressions - Examples of RegEx for Chat

Applies to

Bridge by Smartsheet

What is a Regular Expression

Regular Expressions (RegEx) are special text strings which describe a search pattern.

Let’s start with a basic example of how RegEx work can be shown by extracting the name from a basic sentence like, "Hello, my name is Steven".

Please note that for the tests shown below, we used this online RegEx Tester

The RegEx which is required for this search is the following:

RegEx to extract the name

name is (.*)

Regex Name example

Extract additional information

Now let's make the example more complex by adding another piece of information.

The new sentence will be, "Hello, my name is Steven Black"

Extracting the first name and the last name from this sentence will require a slightly different expression:

RegEx to extract the name and surname

name is (.*) (.*)

The first (.*) will extract the first word after the words "name is" while the second (.*) will extract the next word after the space. Note that there is a space between the two (.*)

In this example we have to separate matches. One for the name and another for the surname:

Regex Extract Additional

 

RegEx and Bridge: from theory to practice.

Let's say we have a customer who wants to book a flight from Edinburgh to Boston and they give us the sentence "Hello, I would like to fly to Edinburgh from Boston".

The way to extract and gather this info would be to use a RegEx as a trigger phrase which will trigger the specific workflow we have built for that purpose.

You can set your trigger phrases by clicking on the Trigger element at the top of a workflow and in the panel that appears, open the section Phrases.

First of all, we need to write down the regex which can extract the 2 cities from the sentence above:

RegEx for itinerary

.*fly to (.*) from (.*)

This expression lets us collect any word which is after the words "fly to" and after "from".

Itinerary

Now that we have the expression, we need to integrate it with Bridge.

The way to use RegEx in Bridge is by adding the prefix "regex:" at the beginning of the field where the expression is needed (in this case in the Phrases section).

As we have a set of information (Boston and Edinburgh in this case) we also need to explain to Bridge how to use that info.

This can be done by adding another part to the original expression:

RegEx with Converse states referrals

| to={.}, from={,}

This part tells Bridge where to store the information extracted from the context.

The section "to" will tell Bridge to store the group match found in the first match (.*) to the state named "to".

At the same time, the section "from" will tell Bridge to store the second group match into the state named "from".

The full expression will be then

Text

regex:.*fly to (.*) from (.*) |to={.},from={,}

In our example, this will be added to the Phrases as shown below:

Add to Phrases

Now add 2 new States and name them "to" and "from".

Other fields in the states do not need to be filled out as the information will be provided directly from the regex we've just added.

If everything has been done properly, the conversation should stop.

Add Two States

To have visual evidence that the states are collecting information from the sentence, just add another state, which for this example we will call "answer", and set the answer field to:

"Cool! You want to fly to {{states.to.value}} from {{states.from.value}}!"

Answer

This will be the result of our conversation:

Result

 

In case you want to enable the bot to also recognize a sentence which, instead of "fly to" --> "from" follows the format "fly from" -- > "to", you will have to add another RegEx to our Phrases list:

Second RegEx phrase

regex:.* fly from (.*) to (.*) | to={,}, from={.}

Additional Example

The state referral, for the second Phrase is to={,} and from={.} as the position in the regex is different from the first one.