regexwatson-conversationwatson-dialog

Retrieve matched text from input.text.matches and store into context variable in watson conversation


I am dealing with IBM Watson Conversation. I have a text that contains few letters and digits i.e. age is 26.

I have written a regex to match the digits from the text. It is done using .*?[0-9]+.*?. Now, I want those matched digits into context variables.

How to place the matched digits into context variable ?

When my condition matches with having input.text.matches('.*?[0-9]+.*?'), then I want to place only digits to my context variable.

For Ex:

{
    "context": {
               "digit": { input.text } 
    }
}

Here input.text takes the whole text and places it into digit variable.

How to place only digits by applying regular expression on text ?


Solution

  • You can extract regular expressions from input text as follows:

    input.text.extract('.*?([0-9]+).*?', 1)
    

    The first part is your regular expression. The second parameter is the group ID.