DataWeave 2.0匹配所有出现的正则表达式

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

我想捕获字符串中与特定正则表达式匹配的所有匹配项。我正在使用DataWeave 2.0(这意味着Mule Runtime 4.3,在我的情况下是Anypoint Studio 7.5)

我曾尝试使用DataWeave核心库中的scan()和match(),但无法完全得到想要的结果。

这是我尝试过的一些事情:

%dw 2.0
output application/json

// sample input with hashtag keywords
var microList = 'Someone is giving away millions. See @realmcsrooge at #downtownmalls now!
#shoplocal and tell them #giveaway @barry sent you. #downtowndancehalls'
---
{
    withscan: microList scan /(#[^\s]*).*/,
    sanitized: microList replace /\n/ 
        with ' ',
    sani_match: microList replace /\n/ 
        with ' ' match /.*(#[^\s]*).*/, // gives full string and last match
    sani_scan: microList replace /\n/ 
        with ' ' scan /.*(#[^\s]*).*/   // gives array of arrays, string and last match
}

以下是各个结果:

{
  "withscan": [
    [
      "#downtownmalls now!",
      "#downtownmalls"
    ],
    [
      "#shoplocal and tell them #giveaway @barry sent you. #downtowndancehalls",
      "#shoplocal"
    ]
  ],
  "sanitized": "Someone is giving away millions. See @realmcsrooge at #downtownmalls now! #shoplocal and tell them #giveaway @barry sent you. #downtowndancehalls",
  "sani_match": [
    "Someone is giving away millions. See @realmcsrooge at #downtownmalls now! #shoplocal and tell them #giveaway @barry sent you. #downtowndancehalls",
    "#downtowndancehalls"
  ],
  "sani_scan": [
    [
      "Someone is giving away millions. See @realmcsrooge at #downtownmalls now! #shoplocal and tell them #giveaway @barry sent you. #downtowndancehalls",
      "#downtowndancehalls"
    ]
  ]
}

在第一个示例中,解析器似乎正在执行行处理。因此,结果数组中每一行都有一个元素。元素由完全匹配的部分和使用模式第一次出现的带标签的部分组成。

在去除换行符之后,第三个示例(sani_match)给了我一个具有完全匹配的部分和带标签的部分的数组,这是该行上最后一次出现该模式。

最终模式(sani_scan)给出相似的结果,唯一的不同是结果被嵌入为数组中的元素。

我想要的是一个简单地包含所有出现的指定模式的数组。

我想捕获字符串中与特定正则表达式匹配的所有匹配项。我正在使用DataWeave 2.0(这意味着Mule Runtime 4.3,在我的情况下为Anypoint Studio 7.5),我已经尝试过...

regex dataweave mulesoft
2个回答
1
投票

如果您想匹配所有“单词”(实际上是非空白字符),与


1
投票

[如果您要捕获字符串中与特定正则表达式匹配的所有匹配项,我发现魔术词是“ Overlapping Matches”。

© www.soinside.com 2019 - 2024. All rights reserved.