LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Match Pattern with shortest string pattern instead of longest string .*

Say,  I have an input like this :  uv tk ab cd ef ab gh jk ab lm op

 

I would like to extract content between the first ab pairs. So with Match pattern and expression is ab .* ab  ,  I got the content between the first ab and the last ab which is not I want.

 

Any suggestions ? I try to use match pattern and not match regex for performance reasons...

 

0 Kudos
Message 1 of 18
(3,817 Views)

Correction,

My input string is uv tk ab cd ef abc gh jk abc lm op   and I would like to get the string between first ab and abc pair.

0 Kudos
Message 2 of 18
(3,792 Views)

data bw strings.png

 

Something like this?

Munna
Message 3 of 18
(3,761 Views)

Simple approach:

- Search fot the first "ab" in your string.

- Then search for the next "ab" in the string starting the search after the first "ab"

- get the StringSubset between both occurances…

 

Surely not as elegant like a RegEx MatchPattern, but realized very quickly…

 

Edit: Munna already shows how to do it!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 18
(3,759 Views)

Yet another possibility would be to use "scan strings for tokens", a very powerful, but under-used function. Just define "ab" as delimiter and you et all the sections between "ab" quickly. If you know it is always the second one, replace the while loop with a FOR loop, set the number of iterations accordingly and don't use autoindexing on the output. (top)

 

You could even use "Spreadsheet string to array" with a delimiter of "ab". (middle)

 

For the smallest match, you could also use your code, but with a patterns of "ab[^ab]+ab", of course the matched string will also contain the ab's, which you would need to trim afterwards. There are probably better ways so some of the regex wizards will hopefully chime in. (bottom)

 

Here is the mentioned code:

 

0 Kudos
Message 5 of 18
(3,680 Views)

@sdfsdfsdfadgadf wrote:

Say,  I have an input like this :  uv tk ab cd ef ab gh jk ab lm op

 

I would like to extract content between the first ab pairs. So with Match pattern and expression is ab .* ab  ,  I got the content between the first ab and the last ab which is not I want.

 

Any suggestions ? I try to use match pattern and not match regex for performance reasons...

 


You can use the following expression: ab[^(ab)]*ab

 

Altenbach, I think you have to put () around ab in the negated character class to avoid a match on a single a or b.

 

Ben64

Message 6 of 18
(3,655 Views)

@ben64 wrote:
You can use the following expression: ab[^(ab)]*ab

That still includes the ab's. He apparently only wants the stuff in-between.

0 Kudos
Message 7 of 18
(3,648 Views)

Yes probably, I was not sure because of what he says in the first post:  "expression is ab .* ab  ,  I got the content between the first ab and the last ab which is not I want"

 

Ben64

0 Kudos
Message 8 of 18
(3,643 Views)

ben64 wrote:

Altenbach, I think you have to put () around ab in the negated character class to avoid a match on a single a or b.


Thanks! Most likely you are right. I don't typically do these things...

 

(Hey, I am a graphical programmer and don't  handle syntax very well. :D)

Message 9 of 18
(3,621 Views)

This is the way to go apparently since .* is greedy and attempt to match the longest string. Unfortunately, I see it is 4 times slower than Spreadsheet string to array although it looks like it is more straightforward.

 

So in conclusion, there is no equivalent operator that attemps non-greedy match (like .+ or something, match zero or more). 

0 Kudos
Message 10 of 18
(3,581 Views)