LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
shb

Extend String Search Functions with "Last Occurence" and "Max Offset"

Status: New

I would like to be able to

  1. find the last occurence (=rfind in C++/python)
  2. Search a match between position 10 and 20

So I propose to extend the search functions with

    1. an input or option "Last Occurence"
    2. an input "Max Offset"

affected search functions:

  • Match Pattern
  • Search and Replace
  • Search/Split String

 

 

Current work arounds:

  1. Last Occurence
    • search all occurences from beginning (with raising offset) until no other is found
    • reverse the string and the match/search string and calculate the real offset out of the return value
  2. Max Offset
    • Search the entire string starting from the offset and check if the match is inside the limit
    • Split the string at the end and search in the new one

This work arounds are not as effective as it could be done in core LabVIEW.

7 Comments
dthor
Active Participant

For Last Occurence, what's wrong with just putting Reverse String in before you execute the search?

 

And for Max Offset, is there any reason why you can't use String Subset and then do the search on the subset?

 

Neither of those two options take up a whole lot of screen space, so I don't see the advantage of your Idea.

shb
Active Participant
Active Participant

I still do see advantages:

This work arounds are not as effective as it could be done in core LabVIEW. When implemented as a function it will be quicker and more memory effective. And when NI realizes it as SubVIs they can probably write more optimized code than me.

 

Not sure if reverse string is optimized by the LabVIEW compiler. Getting a copy of a big string is a disadvantage.

Doing a String Subset and searching on it could produce a copy of the string in the memory. And shoud the offset input of the search VIs be removed? This could also be done with a String Subset. 

 

 

And the code for finding the last occurence looks a bit complicated and is not tiny little:

Find last occurence - Search and Find.png

 

dthor
Active Participant

Ah, you're also looking for the index of the last match, I didn't realise that bit.

Mads
Active Participant

You can make a "replace last occurence" based on search and replace string with a regular expression...Below is such a code with a subVI to escape all special characters in the search and replace strings (in case they contain regex metacharacters):

Replace Last Occurence.png

Escape MetaCharacters.png

wiebe@CARYA
Knight of NI

>Not sure if reverse string is optimized by the LabVIEW compiler. Getting a copy of a big string is a disadvantage.

>Doing a String Subset and searching on it could produce a copy of the string in the memory. And shoud the offset input of the search VIs be removed? This could also be done with a String Subset. 

 

String Subset returns a 'substring'.  Basically a structure with a pointer to the original data and offset\length information. It is very efficient.

 

Reverse string does create copies of memory. It's slow (for large strings).

wiebeCARYA_0-1720087386404.png

 

Mads
Active Participant

I misread the topic here to be a replace of last, but it is a bit related so anyway; I took the code I posted that uses regex and rewrote it with a reverse string instead and it is *much* faster (with very large strings as well), and simpler too:

replace last occurence.png

wiebe@CARYA
Knight of NI

But the point is that:

There will be bugs.

E.g. if no replacement was found: size +1 should be -1)

 

It's inefficient.

With a large string, the reverse is about the same load as the search (which makes sense as it's iterating over the same memory size), so it's ~2X the CPU load. And that's just the "input string".

 

You have to make it.

 

I had the same sentiment with threshold array in reverse - NI Community: inefficient, error prone, and silly you have to make it...