Question

Find the last forward slash in a string

  • 6 December 2019
  • 9 replies
  • 258 views

Badge +4

Hi Community,

I have a simple task I thoughtm but I can't get it to work. I have a long string (URL) with lots of forward slashes. I would like to find the last one or actualy I would like to get the string after the last forward slash. To find the last forward slash I used the @FindString function: @FindString(@Value(exampleURL),"/",-1). It returns -1 instead of the actual place of the slash. When I search from the beginning it works fine: @FindString(@Value(exampleURL),"/") it returns 5 which is the postion of the first slash in an URL.

What am I doeing wrong?

Thank you


9 replies

Userlevel 1
Badge +21

You can use a stringsearcher with the following regex to get the string after the last forward slash

([^\/]+).$
Userlevel 4

You could also simply use the FilenamePartExtractor, the part after the last slash (or backslash) is found in the output attribute _filename.

Badge +3

@arnovananrooij

This grabs the set of characters after the last forward slash.

(?:(?!\\/)).*(?:\\/)(.*)

@ebygomm regexp ("grab everything that is not a forward slash") misses the last character.

It works if you put the dot inside the braces. But you dont need the dot, you can remove it and it works as well.

 

 

Userlevel 1
Badge +21

@arnovananrooij

This grabs the set of characters after the last forward slash.

(?:(?!\\/)).*(?:\\/)(.*)

@ebygomm regexp ("grab everything that is not a forward slash") misses the last character.

It works if you put the dot inside the braces. But you dont need the dot, you can remove it and it works as well.

 

 

Stingsearcher returns the full match not the group match by default so you do get the last character, but probably better to have it right :-)

Userlevel 4
Badge +25

Check out the LastSectionOfStringRemover transformer on the FME Hub. I think that will help do what you want here.

Userlevel 4
Badge +25

Check out the LastSectionOfStringRemover transformer on the FME Hub. I think that will help do what you want here.

Sorry, I just scribbled a short answer because I saw this while I was in the middle of something else. Basically I had the same issue and that's why I came up with the LastSectionOfStringRemover transformer. In fact I think it even defaults to looking for the / character, to prove that's its original purpose.

You can find the transformer on the hub here (https://hub.safe.com/publishers/safe-lab/transformers/lastsectionofstringremover) or just type the name into Quick Add in Workbench and it should download.

If you can think of a better name or description, so that you could have found this transformer to start with, please do let me know.

Badge +3

Stingsearcher returns the full match not the group match by default so you do get the last character, but probably better to have it right :-)

@gio, @ebygomm: You don't need to escape a forward slash (although it doesn't matter if you do)

Badge +3

@ebygomm

Stringsearcher does grabb fullmatch as well as captures. You need to use the advanced bit. (I do not know since which version this is, but it has always been possible using regexp in for instance tcl. I have ancient post here that does that and extracts it using a loop.)

 

@arnold_bijlsma

Yes you do need to escape forward slashes in regular expressions.

You may be confusing non regular expression stringsearches?

 

Badge +3

@ebygomm

Stringsearcher does grabb fullmatch as well as captures. You need to use the advanced bit. (I do not know since which version this is, but it has always been possible using regexp in for instance tcl. I have ancient post here that does that and extracts it using a loop.)

 

@arnold_bijlsma

Yes you do need to escape forward slashes in regular expressions.

You may be confusing non regular expression stringsearches?

 

Interesting! It looks like that in some RegEx flavours, forward slashes will need to be escaped! (Why?)

 

 

However, you don't need to do this in FME. Both the @FindRegEx function and the StringSearcher don't need the forward slash to be escaped. See attached workbench.

 

Also the forward slash is not classed as an official RegEx metacharacter: https://www.regular-expressions.info/refquick.html

Reply