Regex slots

You can use regular expressions in patterns. Regex is helpful in capturing strings with different letter and digit combinations, like unknown names, IDs, flight or license numbers and so on.

To use this slot type, add the * character to the slot name. This will signal Alan this slot contains regex.

For example, if you want to match an intent with the I want $(FLAVOUR) ice cream pattern, but you don’t know all possible flavours in advance, you can use the following expression:

Voice script
const regIce = "\\w+";

intent(`I want $(FLAVOUR* ${regIce}) ice cream`, p => {
    p.play(p.FLAVOUR.value);
});

Note

If you need to use special characters in regex, escape them with \. Such characters are: \, '.

You can use regular expressions of any complexity. In the example below, the intent will match all combinations of six letters and any number of digits after the incident word. This will help if you need your users to input some uncommon abbreviations.

Voice script
let letters_reg = "([A-Za-z]{1}\\s?){6}";
let num_reg = "(\\d+\\s?)+";
let reg = letters_reg+num_reg;

intent(`Incident $(INCIDENT* ${reg})`, p => {
    p.play(`${p.INCIDENT.value}`);
});

Regular expressions work best when they can be uniquely matched. Try to use words surrounding regex in the intent; this will make matching more precise.

Getting any user’s input

To match any user’s input, you can use a greedy regex in slots. This can be helpful, for example, if you need to capture any open phrase like comment or feedback.

In the example below, the user’s message is captured with the COMPLAINT slot and then played back to the user. The (.+) regex pattern here matches any string with one or more characters (a non-empty string).

Voice script
intent('I want to submit a complaint. $(COMPLAINT* .+)', p => {
    p.play(`Your complaint has been registered. You've said: ${p.COMPLAINT.value}`);
});