Skip to main content
Question

Find the last forward slash in a string

  • December 6, 2019
  • 9 replies
  • 1330 views

arnovananrooij
Contributor
Forum|alt.badge.img+5

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

ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • December 6, 2019

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

([^\/]+).$

david_r
Celebrity
  • December 6, 2019

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


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • December 6, 2019

@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.

 

 


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • December 6, 2019
gio wrote:

@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 :-)


mark2atsafe
Safer
Forum|alt.badge.img+44
  • Safer
  • December 6, 2019

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


mark2atsafe
Safer
Forum|alt.badge.img+44
  • Safer
  • December 6, 2019
mark2atsafe wrote:

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.


arnold_bijlsma
Enthusiast
Forum|alt.badge.img+14
ebygomm wrote:

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)


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • December 11, 2019

@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?

 


arnold_bijlsma
Enthusiast
Forum|alt.badge.img+14
gio wrote:

@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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings