In my example, I ask for a personal documentation number, the number has 11 digits, and I need that in case the user type the 11 numbers correctly the watson continue with t
To match numbers with Watson conversation service you can either use the entity sys-number
that can be turned on in the entities tab - but this will match all the numbers and yours is a specific one.
For this use case you can add additional check of the textual user input. The Watson conversation supports regexps checks. If you create a condition of dialog node in this way: input.text.matches('^[^\d]*[\d]{11}[^\d]*$')
then this node will match only if the input.text
which is an accessor to the exact text String that was submitted by the user will match the regular expression (regexp) defined as ^[^\d]*[\d]{11}[^\d]*$
.
This particular expression will match only if there is 11 digits number in the input and no other digits elsewhere, but additional text in front and after the number is allowed.
Now to capture this number to a variable you can add the following to the context
of the dialog node that is matching this number:
"context": {
"number": "<?input.text.extract('^[^\\d]*[\\d]{11}[^\\d]*$',0)?>"
}
Note that there is different escaping of \\d
on the context due to JSON nature of the context
field.
In the output text of a dialog node you can then write something like "Ok, number $number was matched."
to display the number in the chat window.
One more thing - great place with info about regexps where you can also try various type of regular expressions and what they match is Regex 101 web page.