From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular expression - get longest number from string

Solved!
Go to solution
I believe it is easy one but I can't get it.Lets say I have a string 'A1_1000' I want to substract the 1000 using regular expression. When I feed Match regular expression I get '1' which is not the longest number. I know other ways of doing that but I want clean solution in one step. Does anybody knows the right regular expression to accomplish that? Thanks!
LV 2011, Win7
0 Kudos
Message 1 of 10
(4,502 Views)

Hi ceties,

what do you need? Do you need "A1_" or "A1" or ...? Can you explain a bit more?

 

Mike

0 Kudos
Message 2 of 10
(4,493 Views)
No I need to get the longest number, e.g 1000. A1_1000 ->1000; S2_333->333 and so on. I tried [0-9]+ but it always gets the first number following the first letter. Thanks
LV 2011, Win7
0 Kudos
Message 3 of 10
(4,488 Views)

If there's always an underscore _ in front of the number you could search for _[0-9]+ or similar.

 

0 Kudos
Message 4 of 10
(4,485 Views)
It can change it was just an example. But it will always be the longest number i n the string.
LV 2011, Win7
0 Kudos
Message 5 of 10
(4,482 Views)
This is the best solution I was able to come with. I am just wondering if there is "smoother way" without the cycle.
LV 2011, Win7
0 Kudos
Message 6 of 10
(4,478 Views)
Solution
Accepted by topic author ceties

ceties wrote:
This is the best solution I was able to come with. I am just wondering if there is "smoother way" without the cycle.

Since multiple checks are required I would tend to beieve that we do have to loop through the possibilities. in this example

 

 

I start check at offset "0" into the string for a number. Provided i find a number I check if it is longer that any previous number I found and if so save the new longer number in the shift register.

 

Have fun!

 

Ben

Message Edited by Ben on 04-15-2009 09:23 AM
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 7 of 10
(4,447 Views)
Use "[0-9]*" as your regular expression input.

It will match the longest instance with no loop required.

One thing to note, it will see 1000.023453 as two seperate matches.
If you need to deal with floating point numbers, the iterative method seems to be the most robust.
0 Kudos
Message 8 of 10
(4,440 Views)

gsussman wrote:
Use "[0-9]*" as your regular expression input.

It will match the longest instance with no loop required.

One thing to note, it will see 1000.023453 as two seperate matches.
If you need to deal with floating point numbers, the iterative method seems to be the most robust.

Umm, nope.  Did you test this?  When you try it against the examples gived (S1_1000), you'll get an empty string, because your match says "0 or more digits" and the first thing that matches that is an empty string because the first character in the string isn't a digit.  If you're seeing in the help a comment about longest match, or greedy matching, what that means is that if you feed it the string 1000, it will get all 4 characters, not just the first one, even though the first digit on its own (or even the empty string as demonstrated above) is a sufficient match.

0 Kudos
Message 9 of 10
(4,422 Views)
Thanks you all guys for helping me to tame the regular expression the powerfull and scary entity 🙂
LV 2011, Win7
0 Kudos
Message 10 of 10
(4,410 Views)