问题
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