Search code examples
muledataweave

Datawave extract string using regexp


I'm trying to extract a part of a string using a regular expression:

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

Given the string [CODE] (ABC-212) Rest of the title this match (https://regexr.com/557qd)

I used this regexp in my current java application, and now I'm trying to transform this on a Mule App.

Reading the documentation I see that I need to use a Transform, so I setup like this:

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

I stored the string on the var called subject.

Using that regexp on Mule, do not works.. but in my java application does. What I'm doing wrong ?


Solution

  • DataWeave doesn't seem to have a global search flag (the g at the end of the original regex). This means the regular expression has to match the entire string. You use the parenthesis to indicate capture groups that will be returned additionally to the matched string.

    Script:

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

    Output:

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