Search code examples
regexmulemule-studioanypoint-studiomule-esb

Get decimal value from string using Mule Expression Language and Regex


This issue involves Mule's MEL and regex. I have the following JSON payload.

    {
       "balance": "{Amount={BasicAmount=604.45, MinimumAmount=60 445, CurrencyCode=USD}}"
    }

I am able to access the string represented by balance

    {Amount={BasicAmount=604.45, MinimumAmount=60 445, CurrencyCode=USD}}

I need to access the floating point number 604.45 as a string and apply it as a flow variable. I have therefore implemented the following code in my mule application.

    <set-variable variableName="balanceUnrefined" value="#[json:balance]" doc:name="balanceUnrefined"/>
    <logger message="#[flowVars.balanceUnrefined]" level="INFO" doc:name="Logger"/>
    <set-variable variableName="balanceRefined" value="#[regex('\\d+\\.\\d+'.toString(), flowVars.balanceUnrefined)]" doc:name="balanceRefined"/>
    <logger message="#[flowVars.balanceRefined]" level="INFO" doc:name="Logger"/>

The result of the last logger indicates a null. What am I not doing right? Is it the regex?


Solution

  • Following our discussion:

    Use the scan function too. It returns a List<String> so you have to take the first. It works well with your regex on dw 2.0 so should do in de 1.0.

    That my Mule 4 mule-config.xml:

    <ee:set-variable variableName="balance"> <![CDATA[ %dw 2.0 output application/json --- scan(vars.regexpVar, /\d+\.\d+/)[0] ]]> </ee:set-variable> </ee:variables> </ee:transform> <logger doc:id="b458c5f0-5360-40f4-b68c-b5cf1e48da51" doc:name="Logger" doc:timestamp="1518874066833" message="#[vars.balance]"/>
    

    And here my result logs:

    enter image description here