Dataweave 使用正则表达式提取字符串
Datawave extract string using regexp
我正在尝试使用正则表达式提取字符串的一部分:
\[CODE\] \((.*?)\)
给定字符串 [CODE] (ABC-212) Rest of the title
这个匹配 (https://regexr.com/557qd)
我在我当前的 java 应用程序中使用了这个正则表达式,现在我正试图在 Mule 应用程序上转换它。
阅读文档我发现我需要使用转换,所以我这样设置:
%dw 2.0
output application/json
---
vars.subject match(/\[CODE\] \((.*?)\)/)
我将字符串存储在名为 subject
的 var 中。
在 Mule 上使用该正则表达式不起作用..但在我的 java 应用程序中却起作用。我做错了什么?
DataWeave 似乎没有全局搜索标志(原始正则表达式末尾的 g)。这意味着正则表达式必须匹配整个字符串。您使用括号来指示将额外返回到匹配字符串的捕获组。
脚本:
vars.subject match(/^\[CODE\] \((.*)\).*/)
输出:
[
"[CODE] (ABC-212) Rest of the title",
"ABC-212"
]
Match in DataWeave returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex. And in your case "Rest of the title" was not matching as per per Regex provided by you.
%dw 2.0
output application/json
var subject="[CODE] (ABC-212) Rest of the title"
---
//Catch here is, Regex should be fully matching as per input,
{
matchesOnly:subject matches /(\[CODE\]) \((.*?)\)[\w\W]*/,
matchOnly: subject match /(\[CODE\]) \((.*?)\)[\w\W]*/
}
我正在尝试使用正则表达式提取字符串的一部分:
\[CODE\] \((.*?)\)
给定字符串 [CODE] (ABC-212) Rest of the title
这个匹配 (https://regexr.com/557qd)
我在我当前的 java 应用程序中使用了这个正则表达式,现在我正试图在 Mule 应用程序上转换它。
阅读文档我发现我需要使用转换,所以我这样设置:
%dw 2.0
output application/json
---
vars.subject match(/\[CODE\] \((.*?)\)/)
我将字符串存储在名为 subject
的 var 中。
在 Mule 上使用该正则表达式不起作用..但在我的 java 应用程序中却起作用。我做错了什么?
DataWeave 似乎没有全局搜索标志(原始正则表达式末尾的 g)。这意味着正则表达式必须匹配整个字符串。您使用括号来指示将额外返回到匹配字符串的捕获组。
脚本:
vars.subject match(/^\[CODE\] \((.*)\).*/)
输出:
[
"[CODE] (ABC-212) Rest of the title",
"ABC-212"
]
Match in DataWeave returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex. And in your case "Rest of the title" was not matching as per per Regex provided by you.
%dw 2.0
output application/json
var subject="[CODE] (ABC-212) Rest of the title"
---
//Catch here is, Regex should be fully matching as per input,
{
matchesOnly:subject matches /(\[CODE\]) \((.*?)\)[\w\W]*/,
matchOnly: subject match /(\[CODE\]) \((.*?)\)[\w\W]*/
}