0

I have a requirement to extract data from a string after the occurence of '@'. Example abc@123456 should result in 123456. I am doing htis in DataWeave Mule.

Kindly suggest

2 Answers2

2

You should use splitBy for this, then grab what you need by index:

%dw 1.0
%output application/java

%var data = ("abc@123456" splitBy "@")[1]
---
data

This will set the payload to String: "123456"

jerney
  • 2,187
  • 1
  • 19
  • 31
2

Alternatively, you can use the regular expression /.*@(.*)/ to find the last occurrence of @ and return the group that follows, regardless of how many @ symbols occur in the input string.

%dw 1.0
%output application/json

%var regex = /.*@(.*)/

%var data1 = ("abc@123456" match regex)[1]
%var data2 = ("xyz@abc@123456" match regex)[1]
%var data3 = ("xy@@z@abc@123456" match regex)[1]
%var data4 = ("name@example.com" match regex)[1]
---

result: { 
    data1: data1,
    data2: data2,
    data3: data3,
    data4: data4 
}

This gives the following results:

{
  "data1": "123456",
  "data2": "123456",
  "data3": "123456",
  "data4": "example.com"
}
Alex Theedom
  • 1,604
  • 15
  • 16