0

I have a 'relative-filename' parameter in a Spring Integration Message Header.

It could be formatted like

"/tmp/frodo/files/pending/whatever.xml" or "/tmp/wilfred/files/completed/new/another.xml"

The portion before "files" can be of variable length...

I was using an expression like:

headers['relative-filename'].substring(11,) to get the part of the string including and after "files" ("files" is always there) .... this works for "frodo", but not for "wilfred".

How do I get the part of the string after "files"?

Can one have a variable expression as the first index of the "substring" or is there a regex that will prune the first portion of the file?

Thanks for any ideas.

2 Answers2

0

This regex will work for all paths having two names before the files part, regardless of their lenght:

^(\/.*?\/.*?\/)files(.*)$

Working DEMO

Enissay
  • 4,969
  • 3
  • 29
  • 56
  • I am not a big "regex" person (I haven't studied them as in depth as I should and I REALLY should do this), but this is a really elegant solution. Thanks. It fits very nicely in the SpEl that I need. – Stephen McConnell Nov 26 '13 at 15:32
0

You can use indexOf() and length() in your expression. 7 is a length of searched text. This is an example:

 before: "'a/files/b'.substring(0, 'a/files/b'.indexOf('/files/'))"
 after: "'a/files/b'.substring('a/files/b'.indexOf('/files/') + 7, 'a/files/b'.length())"
jonasnas
  • 3,540
  • 1
  • 23
  • 32