Skip to main content
Question

Reverse search using string function FindString() (negative startIndex?)


thijsknapen
Contributor
Forum|alt.badge.img+10

Say I have data like the following;

sequence_path (string: UTF-8): 1.2.3.4

As a result I wanted to fetch the parent from this sequence_path (i.e. '1.2.3'). 

In order to do so I wanted to do the following;

  • Use a reverse search to find the position of the last '.' in the string, 
  • Fetch the (Left) substring up to the position of this last '.'

 

To perform the first step, I thought to use the FindString() string function (with a negative startIndex). 

So, at first I thought that @FindString(@Value(sequence_path),.,-1) could be used. However, that returned '-1' (not found). However, somewhat unexpectedly at first, I noticed that '@FindString(@Value(sequence_path),.,-2) ' did return the desired value.

 

Upon second reading of the String Functions documentation: "If startIdx is a negative integer, FindString() returns the index in str starting at startIdx from the end of string , then matching strToFind going forward (from left to right)."

 

I now notice that the search is still from left to right (forward), however only from the negative index onward. 

E.g. '@FindString(@Value(sequence_path),.,-3)', seems to look for a dot in '3.4', to find it in the second spot (index '1'), and then return 5 as the result (5 = 7-3+1 = StringLength + startIndex + matchIndex).

 

Is it possible to perform a reverse search (search from right to left) in a (non-Regex) StringSearcher function/transformer?

 

ps. from an efficiency perspective, I would like to avoid StringSearches that rely on Regular Expressions.

2 replies

geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • July 17, 2023

You can always reverse the string or use the StringReverser (FME Hub)😁

 


thijsknapen
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • July 19, 2023

Tnx for the idea geomancer. For my current usecase I decided to use @TrimRight(@TrimRight(@Value(sequence_path),0123456789),.)

 

My question was inspired by the observation that the Oracle SQL instr function has the option to perform a reverse search, but SQLite SQL instr function does not. To work around this in SQLite SQL I already found this workaround; https://stackoverflow.com/questions/21388820/how-to-get-the-last-index-of-a-substring-in-sqlite).

I was just wondering if FME StringSearcher functions would support reverse searching, but I guess not.

 

My question was mainly from an efficiency perspective (nr of features could get up to millions), and I have some worries that introducing an additional step of reversing the string won't be more efficient than actually using Regular Expression searches.


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