Regular Expressions Are Still Useful For Chatbots

Regular Expressions Are Still Useful For Chatbots

Using Regular Expressions With LUIS & Bot Framework Composer


The concept formed during the 1950’s by an American mathematician Stephen Cole Kleene. He formalized the description of a regular language.

Image for post
Regex Entities in LUIS

A Regular expression (regex) is a sequence of characters defined by a pattern.

A regex is a standard textual syntax which represents a pattern we want to match in our text.

A degree of flexibility can be introduce by using wildcards.

The entity is a good fit when:

  • The data are consistently formatted with any variation that is also consistent.
  • The regular expression does not need more than 2 levels of nesting.

A good way to think of employing Regex in your chatbot is where you want to extract an entity which have a limited format…The format of the number might be very constant, but with high variances of letters of numbers.

Examples of such numbers are:

  • Reference Numbers
  • Flight Numbers
  • Ticket Reference Numbers etc.

An added advantageous of Regular Expressions is the fact that is is lightweight and a compact way to extract specific information. You do not have to worry about training examples, contextual awareness or specific intents necessarily.

Using Regex in Composer & LUIS

As a practical example let us look at combining Microsoft Bot Framework Composer integrated with LUIS.

Image for post
Microsoft Bot Framework Composer View

The LUIS .lu format can be formatted and defined within the Composer dialog.

Composer does vet and check the .lu format you have defined in the Expected Responses.

Once you restart the bot, the configuration is deployed to LUIS in the cloud.

To make sure, log into your LUIS instances and check intents and entities. Your trigger phrases will be listed under intents and the regular expression defined is under entities.

Image for post
Dialog to test the regular expression input

Here is the simple call flow designed for this demo; the dialog introduces itself, and asks for a flight number. The flight number must start with the “hrf-” followed by six digits, between zero and nine.

- \# Regex@ regex fnumber from, to = /hrf-[0-9]{6}/

Th user input after the bot asked a question, is shown below…

Image for post
Composer User Input with Regex code for LUIS consumption

The result can be imbedded in the chatbot dialog response:

- I have your flight number as ${dialog.fnumber}

Imbedding information within the chatbot’s dialog is import for a conversational feel to the chatbot.

Testing Our Bot

Again testing can be performed via the Bot Framework Emulator. After changes, it is advisable to restart the conversation. The emulator can be started from within Composer, and if installed, will launch the instance.

Image for post
Microsoft Bot Framework Emulator — Test Sequence

Within Emulator the conversation can be debugged and the integration portion with LUIS can be reviewed. This is helpful for troubleshooting and finding interpretation and response issues.

"recognizerResult": {
"alteredText": null,
"entities": {
"fnumber": [
"intents": {
"FlightDetail": {
"score": 0.208026841
"text": "my flight num is hrf-340394"

This is the JSON format returned by LUIS with defined intents and entities.


LUIS is a flexible environment for creating a NLU API and publishing it…Subsequently Composer is a good environment to create a dialog and a state machine sequence to consume the NLU API and make a chatbot interface available to users.

This environment is also efficient for prototyping and creating demos.

4 January 2021

Leave a Reply

Your email address will not be published. Required fields are marked *