Datawave使用regexp提取字符串

问题描述 投票:0回答:1

我试图使用正则表达式来提取一个字符串的一部分。

\[CODE\] \((.*?)\)

给出这个字符串 [CODE] (ABC-212) Rest of the title 本场比赛(https:/regexr.com557qd)

我在我当前的java应用中使用了这个regexp,现在我想在Mule应用中转换这个。

阅读文档时,我看到我需要使用一个Transform,所以我这样设置。

%dw 2.0
output application/json
---
vars.subject match(/\[CODE\] \((.*?)\)/)

我把这个字符串存储在名为 subject.

在Mule上使用该regexp,不工作......但在我的java应用中却可以。我做错了什么?

mule dataweave
1个回答
1
投票

DataWeave似乎没有全局搜索标志(原始regex末尾的g)。这意味着正则表达式必须匹配整个字符串。你用括号来表示捕获组,这些组将在匹配的字符串之外被返回。

脚本。

vars.subject match(/^\[CODE\] \((.*)\).*/)

脚本:输出。

[
  "[CODE] (ABC-212) Rest of the title",
  "ABC-212"
]

1
投票

enter image description here

DataWeave中的Match返回一个数组,这个数组包含了 全文在您的案例中,"标题的其余部分 "没有按照您提供的Regex匹配。而在您的案例中,"标题的其余部分 "与您提供的Regex不匹配。

%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]*/
}
© www.soinside.com 2019 - 2024. All rights reserved.