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: 

regex find number at end of string

Solved!
Go to solution

Hi guys,

 

I try to learn regex, but it's hard and I'm stuck at a simple task:

 

Let's say I have this string:

 

"C1"

 

I would like to find what the number on the end of the line is. I can do it with this regex "[0-9]+$".

 

Now if this string has been entered with an small error, let's say a "space" character after the number:

 

"C1 "

 

My regex will not return "1".

 

What should I change in my regex to find the last number in the string even if it has an error?

 

P.S. The string can contain multiple numbers, like "C5_IND1". My regex should return "1".

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 1 of 11
(29,066 Views)

Regex (find number from last).png

Change it to "[0-9.]+$"


I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


0 Kudos
Message 2 of 11
(29,061 Views)

Thanks for answering, but it does not quite fix my problem:

x.PNG

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 3 of 11
(29,053 Views)
Solution
Accepted by topic author EvenDeejay

Use look aheads.  I sounds like you want the last number which is not followed by another number:

(\d+)(?!.*\d)

Message 4 of 11
(29,037 Views)

Thanks, Don,

 

It works as a charm=)

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 5 of 11
(29,022 Views)

@Don_Phillips wrote:

Use look aheads.  I sounds like you want the last number which is not followed by another number:

(\d+)(?!.*\d)


So this is a great start but what about numbers that are decimals?

 

Also is it possible to find the first number and not the last?

0 Kudos
Message 6 of 11
(29,000 Views)

Well by default regex takes the first match so if you want the first number (and not the last) just use \d+


2013-08-01 12_33_34-Untitled 1 Front Panel _.png

 

If you have decimals in your numbers (or a mix) that would make it more complicated but it can still be done.

A pattern for a whole number (no decimal) or a number with a decimal would be:

 

\d+(\.\d+)?

 

So by extension the last number with or without a decimal should be:

 

(\d+(\.\d+)?)(?!.*\d+(\.\d+)?)

 

Message 7 of 11
(28,988 Views)

In my case there cannot be a decimal number, but I'm glad the question came up:) Always good to know a little more regex.

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 8 of 11
(28,967 Views)

This is fantastic.  There are many times where maybe some other software will generate a text file but have an inconsistent number of spaces between columns.  There are many ways to format it correctly but this seems like an easy way to just loop over one row at a time finding numbers seperated by anything that isn't a number.

0 Kudos
Message 9 of 11
(28,949 Views)

So I've been using this regular expression for a while now and I like it but I always heard regular expressions were an easy but less inefficient than native G code.  So I've implemented what I think is a "Just find me the numbers" functions and wrapped them into polymorphic VIs to make them easier to use.  I also tried a Match Regular Expression version but it wasn't as good but that could have been my code to be fair.

 

This will find the first decimal number, or last number is a string.  But it also can find the first number in a multi row string, or last number.  It can also find all numbers in all rows of a string.  It can also support inputs of 1D and 2D arrays.

Message 10 of 11
(18,817 Views)