如何在 Watson 中使用 getMatch 获取两个字符串之间的文本
How to get text between two string with getMatch in Watson
我正在研究 Ibm Watson 助手以获得可理解的结构。
其中之一是检索两个单词之间的字符串。
句子很像;
application song name çalar mısın?
还有
application'x song name çalar mısın?
我想在这句话中得到 "song name"。歌名随便起。
正则表达式在此页面上运行良好:regexr.com/4ltp1
"PLAY_SONG": "<? input.text.getMatch('(?=(?!application)\s).+(?=\s+çal)',0) ?>"
当我尝试执行时出现错误,如下所示。
使用对话节点 ID [handler_3_1569393700920] 的上下文更新上下文时出错。对话节点上下文是 [{"category":"action","PLAY_SONG":""}]。正则表达式模式语法错误:解析正则表达式时出错:无效或不受支持的 Perl 语法:(?=
这种情况下正确的做法是什么?
您使用的正则表达式引擎不支持回顾。累积Watson Assistant docs 正则表达式引擎是 RE2,它既不支持后向也不支持先行。
在这里,您可以通过使用嵌套前瞻删除正后视并依赖 capturing group using the extract
method:
来解决问题
String.extract(String regexp, Integer groupIndex)
This method returns a string from the input that matches the regular expression group pattern that you specify. It returns an empty string if no match is found
使用
"PLAY_SONG": "<? input.text.extract('application\S*\s+(.*?)\s+çal', 1) ?>"
注意 1
参数,它告诉 getMatch
到 return 用带括号的正则表达式部分捕获的匹配部分。
详情
application
- 文字子串
\S*
- 除空格外的 0+ 个字符
\s+
- 1+ 个空格
(.*?)
- 捕获第 1 组:任何 0+ 个字符,尽可能少
\s+
- 1+ 个空格
çal
- 文字子串。
我正在研究 Ibm Watson 助手以获得可理解的结构。 其中之一是检索两个单词之间的字符串。
句子很像;
application song name çalar mısın?
还有
application'x song name çalar mısın?
我想在这句话中得到 "song name"。歌名随便起。
正则表达式在此页面上运行良好:regexr.com/4ltp1
"PLAY_SONG": "<? input.text.getMatch('(?=(?!application)\s).+(?=\s+çal)',0) ?>"
当我尝试执行时出现错误,如下所示。
使用对话节点 ID [handler_3_1569393700920] 的上下文更新上下文时出错。对话节点上下文是 [{"category":"action","PLAY_SONG":""}]。正则表达式模式语法错误:解析正则表达式时出错:无效或不受支持的 Perl 语法:(?=
这种情况下正确的做法是什么?
您使用的正则表达式引擎不支持回顾。累积Watson Assistant docs 正则表达式引擎是 RE2,它既不支持后向也不支持先行。
在这里,您可以通过使用嵌套前瞻删除正后视并依赖 capturing group using the extract
method:
String.extract(String regexp, Integer groupIndex)
This method returns a string from the input that matches the regular expression group pattern that you specify. It returns an empty string if no match is found
使用
"PLAY_SONG": "<? input.text.extract('application\S*\s+(.*?)\s+çal', 1) ?>"
注意 1
参数,它告诉 getMatch
到 return 用带括号的正则表达式部分捕获的匹配部分。
详情
application
- 文字子串\S*
- 除空格外的 0+ 个字符\s+
- 1+ 个空格(.*?)
- 捕获第 1 组:任何 0+ 个字符,尽可能少\s+
- 1+ 个空格çal
- 文字子串。