简体   繁体   中英

Extract string from message using Dataweave

I want to use Dataweave to pull out the text between the first : and first ( which in this case is "File incorrect" but in other cases it maybe different.

java.lang.Exception: File incorrect (javax.script.ScriptException). (org.mule.api.transformer.TransformerMessagingException).

I have looked at some of the string manipulation available and looks like you could only use it if you know the position of the text or the text you want to extract.

How can I do this?

Thanks

U can extract it using regex and match function in datatweave. Try the below Dwl

%dw 1.0

%var exceptionMsg = 'java.lang.Exception: File incorrect( javax.script.ScriptException). (org.mule.api.transformer.TransformerMessagingException).'
%output application/json
---
trim (exceptionMsg match /^(.*):([^(]*).*$/)[2]

You can make a call to groovy function from your Dateweave to extract the required string like bellow:

<configuration doc:name="Configuration">
<expression-language autoResolveVariables="false">
<global-functions>
def extractmsg(messageString) {
    int firstindex = messageString.indexOf(":");
    int endindex = messageString.indexOf("(");
    String msg = messageString.substring(firstindex+1, endindex).trim();
    return msg;
}
</global-functions>
</expression-language>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM