08-01-2013 07:40 AM
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".
Solved! Go to Solution.
08-01-2013 07:47 AM
Change it to "[0-9.]+$"
08-01-2013 07:55 AM
Thanks for answering, but it does not quite fix my problem:
08-01-2013 08:11 AM
Use look aheads. I sounds like you want the last number which is not followed by another number:
(\d+)(?!.*\d)
08-01-2013 08:43 AM
Thanks, Don,
It works as a charm=)
08-01-2013 11:30 AM
@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?
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
08-01-2013 12:45 PM
Well by default regex takes the first match so if you want the first number (and not the last) just use \d+
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+)?)
08-02-2013 12:45 AM
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.
08-02-2013 09:30 AM
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.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
04-15-2019 04:02 PM
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.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord