4

I have a variable my_variable with value as a dynamic URL like --

http://stackoverflow.com/questions/ask

I want to do a substring on this dynamic URL to find the string after last "/" i.e. in case above mentioned URL, I want to get the substring "ask"

How can I use MEL to do that?

user1493140
  • 5,976
  • 4
  • 29
  • 49

4 Answers4

5

You can use the string functions which are availalbe from the java.lang package.

#[flowVars['my_variable'].substring(flowVars['my_variable'].lastIndexOf('/'))]

Hope this helps.

user1760178
  • 6,277
  • 5
  • 27
  • 57
2

You can use JDK classes in MEL, in fact these packages are auto imported (http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Reference):

  • java.lang.*
  • java.io.*
  • java.net.*
  • java.util.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • javax.activation.DataHandler
  • javax.activation.MimeType
  • java.util.regex.Pattern
  • org.mule.api.transformer.DataType
  • org.mule.transformer.types.DataTypeFactory

This flow receives requests on port 8081 and return the last part of the path:

<flow name="testedbFlow3">
    <http:inbound-endpoint host="0.0.0.0" port="8081" />
    <expression-transformer expression="#[message.inboundProperties['http.request'].split(&quot;^.*/&quot;)[1]]"/>
</flow>
Ale Sequeira
  • 2,039
  • 1
  • 11
  • 19
1

You can use basic Java String methods such as substring() and lastIndexOf() on the variable.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Anton Kupias
  • 3,945
  • 3
  • 16
  • 20
0
#[flowVars['my_variable'].substring(flowVars['my_variable'].lastIndexOf('/'))]

which is simpler one

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Sudha Ch
  • 127
  • 1
  • 7
  • This is the same as @user1760178's answer, except yours is missing the necessary pound sign. – Tony Dec 08 '17 at 05:27
  • 1
    While this code may answer the question, providing additional context regarding **how** and/or **why** it solves the problem would improve the answer's long-term value. – Alexander Apr 03 '18 at 16:56