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

白昼怎懂夜的黑 提交于 2019-12-11 06:05:25

问题


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 ?


回答1:


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.




回答2:


The second parameter is required for the extraction to work, so if we want to extract the whole matched substring, we use 0 for the extract_id as follows:

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


来源:https://stackoverflow.com/questions/39789683/retrieve-matched-text-from-input-text-matches-and-store-into-context-variable-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!