Code:
controller.hears([map (.*) (.*) into (.*)){
var value = message.match[1];
var source = message.match[2];
var dest = message.match[3]; }
when input is map 12 grams to kilograms
it works fine but when input is map 12 square meter into square centimeter
then output is
value = 12 square, source= meter and dest = square centimeter
How to modify the code so that the value field accepts only number so that square meter goes to source field
You may use
map (\d+) (.*) into (.*)
See the regex demo
The first capturing group is now (\d+)
and will only capture 1 or more digits.
You may also match 1+ whitespaces in between values using \s+
(so, tabs and other whitespace will also be matched, 1 or more occurrences):
map\s+(\d+)\s+(.*?)\s+into\s+(.*)
See this regex demo.
Details
map
- a map
substring\s+
- 1+ whitespaces(\d+)
- Group 1: one or more digits(.*?)
- Group 2: any 0+ chars other than line break chars, as few as possible, \s+
- 1+ whitespacesinto
- into
substring\s+
- 1+ whitespaces(.*)
- Group 3: any 0+ chars other than line break chars, as many as possible.