John Lokanis Posted October 9, 2008 Report Share Posted October 9, 2008 I am trying to create a function that will test a string to see if it contains non-numeric chars. So, I need to output a TRUE if the string only contains the characters 0 through 9 and . Whitespace should be ignored. 1.23 = TRUE 123-456 = FALSE 1.23A = FALSE A123 = FALSE 123 = TRUE etc... Is there an easy way to do this or do I need some complex code with loops and match extpression calls for each char in the string? thanks, -John Quote Link to comment
TobyD Posted October 9, 2008 Report Share Posted October 9, 2008 QUOTE (jlokanis @ Oct 8 2008, 10:56 AM) Is there an easy way to do this or do I need some complex code with loops and match extpression calls for each char in the string? You can do this with Match Regular Expression. Read up on filtering with regular expressions and you can build an expression that will search for any non numeric character. If none are found the VI returns -1. If I get some more time I'll see if I can make it work. EDIT: OK, If you use [^0-9 ] as your regular expression, the VI will return a positive number if any non numeric character other than a space is used. If it is all numeric, it returns -1. Quote Link to comment
jdunham Posted October 9, 2008 Report Share Posted October 9, 2008 QUOTE (jlokanis @ Oct 8 2008, 10:56 AM) I am trying to create a function that will test a string to see if it contains non-numeric chars. So, I need to output a TRUE if the string only contains the characters 0 through 9 and .Whitespace should be ignored. Well it's pretty easy to write in LabVIEW without regular expressions But make sure that you don't want to support scientific notation or NaN, and what about a + or - only at the beginning. You should also use trim whitespace.vi rather than testing for whitespace chars if you don't want to allow whitespace in the middle. If you really want to do it right, a regular expression may be better. check out http://www.regular-expressions.info/floatingpoint.html Quote Link to comment
Francois Normandin Posted October 9, 2008 Report Share Posted October 9, 2008 Just a side note to point out that comma are used in some countries instead of points... 1 Quote Link to comment
John Lokanis Posted October 9, 2008 Author Report Share Posted October 9, 2008 QUOTE (TobyD @ Oct 8 2008, 11:27 AM) If you use [^0-9 ] as your regular expression, the VI will return a positive number if any non numeric character other than a space is used. If it is all numeric, it returns -1. Unfortunatly, [^0-9] does not account for the decimal point. Do you know if a regular expression can be written that says "not 0-9 AND not ." ?? I would prefer to use a regular expression if possible because that should be faster, but the native LV code above looks like it would work too. -John QUOTE (jlokanis @ Oct 8 2008, 01:58 PM) Unfortunatly, [^0-9] does not account for the decimal point. Do you know if a regular expression can be written that says "not 0-9 AND not ." ?? Ah, but [^0-9.] does work! WooHoo! I really need to get a good book on regular expression writting... Quote Link to comment
TobyD Posted October 9, 2008 Report Share Posted October 9, 2008 QUOTE (jlokanis @ Oct 8 2008, 02:27 PM) Unfortunatly, [^0-9] does not account for the decimal point. Do you know if a regular expression can be written that says "not 0-9 AND not ." ?? I think you can use [^0-9 \.] (you might not need the backslash). This will allow multiple decimal points though i.e. 12.342.12.23 I'm just learning regular expressions, but there are some great websites with examples of how to do just about anything. If you need to limit your check to a single decimal, I'm sure there is a way to do that but I'd have to spend some time digging. EDIT: Well, I'm too slow, but don't forget to add a space in there if you want to ignore white space. :thumbup: Quote Link to comment
jpdrolet Posted October 10, 2008 Report Share Posted October 10, 2008 The expression ^[ ]*[+-]?[0-9]+\.?[0-9]*$ should match a floating point number ^[ ]* : any number of spaces at the beginning [+-]? : 0 or 1 sign [0-9]+ : at least one digit \.? : 0 or 1 decimal point [0-9]*$ : any digit up to the end It will not recognize a number beginning with a point like .8 but will reject the single . without any digit Quote Link to comment
rkanders Posted October 10, 2008 Report Share Posted October 10, 2008 ^(?:\d+(?:\.\d*)?|\.\d+)(?:\d+)?$ this will also recognize .8 I am pretty happy that finally LV supports regexes, makes life much easier. I use regex buddy to test expressions, here is the explanation it gave for the above regex: Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» Match the regular expression below «(?:\d+(?:\.\d*)?|\.\d+)» Match either the regular expression below (attempting the next alternative only if this one fails) «\d+(?:\.\d*)?» Match a single digit 0..9 «\d+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match the regular expression below «(?:\.\d*)?» Between zero and one times, as many times as possible, giving back as needed (greedy) «?» Match the character “.” literally «\.» Match a single digit 0..9 «\d*» Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» Or match regular expression number 2 below (the entire group fails if this one fails to match) «\.\d+» Match the character “.” literally «\.» Match a single digit 0..9 «\d+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match the regular expression below «(?:\d+)?» Between zero and one times, as many times as possible, giving back as needed (greedy) «?» Match a single digit 0..9 «\d+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Assert position at the end of a line (at the end of the string or before a line break character) «$» Created with RegexBuddy Reinis QUOTE (jpdrolet @ Oct 8 2008, 08:07 PM) The expression ^[ ]*[+-]?[0-9]+\.?[0-9]*$ should match a floating point number^[ ]* : any number of spaces at the beginning [+-]? : 0 or 1 sign [0-9]+ : at least one digit \.? : 0 or 1 decimal point [0-9]*$ : any digit up to the end It will not recognize a number beginning with a point like .8 but will reject the single . without any digit Quote Link to comment
vugie Posted October 10, 2008 Report Share Posted October 10, 2008 For such cases I usually use Scan From String function and check whether error appeared. You can also check whether the rest of string contain something more than whitespace or use Scan From String in a loop until rest of string is empty Quote Link to comment
jpdrolet Posted October 10, 2008 Report Share Posted October 10, 2008 QUOTE (vugie @ Oct 9 2008, 04:58 AM) For such cases I usually use Scan From String function and check whether error appeared. You can also check whether the rest of string contain something more than whitespace or use Scan From String in a loop until rest of string is empty We have a clear winner here. :thumbup: Quote Link to comment
jzoller Posted October 10, 2008 Report Share Posted October 10, 2008 QUOTE Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski Scan from String sounds good to me too! Joe Z. Quote Link to comment
Clio75 Posted March 28, 2009 Report Share Posted March 28, 2009 QUOTE (normandinf @ Oct 8 2008, 08:08 PM) comma are used in some countries instead of points... Ye I know. Have this problem EVERY time I use a new computer. It's f... annoying. Quote Link to comment
Rolf Kalbermatter Posted March 30, 2009 Report Share Posted March 30, 2009 QUOTE (Clio75 @ Mar 27 2009, 04:37 PM) Ye I know. Have this problem EVERY time I use a new computer. It's f... annoying. Scan from String will not have this problem. And if you know you could have numbers with a specific decimal point instead of the current system decimal point (instrument responses for instance) you just prepend the %.; to the format string. Rolf Kalbermatter Quote Link to comment
buckidge Posted September 25, 2014 Report Share Posted September 25, 2014 Sounds like scan from string universally works too, but I have code to convert to a number and back to a string. If they match, I consider it a valid numeric: convert numeric string.vi 1 Quote Link to comment
Gepponline Posted March 6, 2015 Report Share Posted March 6, 2015 Hi! i need to check an integer string, but if i use Scan from string with a %d as format string, and I have a string that contains 12A13 for example, it don't return me an error 'cause it recognize a 12 before A. SHould i use a different format string to consider the whole string? Quote Link to comment
Gepponline Posted March 6, 2015 Report Share Posted March 6, 2015 I've found this on wikipediahttp://rosettacode.org/mw/images/f/fb/LabVIEW_Determine_if_a_string_is_numeric.png Quote Link to comment
ShaunR Posted March 6, 2015 Report Share Posted March 6, 2015 Hi! i need to check an integer string, but if i use Scan from string with a %d as format string, and I have a string that contains 12A13 for example, it don't return me an error 'cause it recognize a 12 before A. SHould i use a different format string to consider the whole string? I've never gotten the scan from string to work, ever (and I have tried ). I have a fundamental misunderstanding of how it works and if it works how I think it might, then it's of no use to me. I think it is probably misnamed and should be called "Extract Literally From String". Quote Link to comment
Rolf Kalbermatter Posted March 6, 2015 Report Share Posted March 6, 2015 Hi! i need to check an integer string, but if i use Scan from string with a %d as format string, and I have a string that contains 12A13 for example, it don't return me an error 'cause it recognize a 12 before A. SHould i use a different format string to consider the whole string? Seems to me you want to scan a hexadecimal integer. That would be %x. Quote Link to comment
ned Posted March 6, 2015 Report Share Posted March 6, 2015 Seems to me you want to scan a hexadecimal integer. That would be %x. No, he wants the Scan from String to fail because the string contains a non-numeric character. Quote Link to comment
Darin Posted March 6, 2015 Report Share Posted March 6, 2015 Typically I would use a regex to validate a number, for a decimal number you could use ^[+-]?[0-9]+$ with Match Pattern to perform validation. In this case you could also verify that the remaining string is empty when you use Scan from String. 1 Quote Link to comment
Rolf Kalbermatter Posted March 8, 2015 Report Share Posted March 8, 2015 No, he wants the Scan from String to fail because the string contains a non-numeric character. Well, Scan from String is not meant to be used like that, just as the scanf() functions in C. They consume as many characters as match the format specifier and then stop. They only fail if no character matches the format specifier or if literal strings in the format string don't match. While I would usually use Match Pattern for such things, Scan From String can be used too. If you want to know if the whole string was consumed you just have to check that the offset past scan is equal to the string length just as Darin suggested or that the remaining string is empty. Quote Link to comment
eberaud Posted March 9, 2015 Report Share Posted March 9, 2015 Typically I would use a regex to validate a number, for a decimal number you could use ^[+-]?[0-9]+$ with Match Pattern to perform validation I've always found that the LabVIEW help didn't explain well enough how the Match Pattern and the Match Regular Expression work. Do you know a good source for learning more about how to use them properly? Thanks Quote Link to comment
ThomasGutzler Posted March 9, 2015 Report Share Posted March 9, 2015 I've always found that the LabVIEW help didn't explain well enough how the Match Pattern and the Match Regular Expression work. Do you know a good source for learning more about how to use them properly? Thanks Regular expressions can be difficult and every language seems to have them implemented slightly different. The basics are the same and they are explained in the LabVIEW help. For more info you can try the Regular Expressions Cookbook or any of the 20700 Google matches for "perl compatible regular expressions cheat sheet". And when you have finally mastered them, you can achieve anything! There's one thing I'd like to show that possibly not everyone knows. Rather than cascading matches and using the "whole match" terminal, you can extend the Match Regular Expression node to get access to submatches like this: 2 Quote Link to comment
eberaud Posted March 10, 2015 Report Share Posted March 10, 2015 Wow I didn't even know you could extend it! Thanks for the help Thomas Quote Link to comment
hooovahh Posted March 23, 2015 Report Share Posted March 23, 2015 Okay so here is the VI I came up with which is similar to the one jdunham posted earlier but this has a few improvements. It finds the local decimal separator (discussion here). It only allows one decimal separator. It only allows white space in the beginning or the end. I feel a space between numbers is no longer a number because the String to Number conversions don't work right if there is. I added support for Hex or Decimal strings. Oh and this version supports things like ".1" where the one posted by buckidge would not work correctly. It does not use regular expression but generally regular expression is slower than custom code. I could be wrong so post a version that does this with regular expression and we can compare. Saved in 2013. EDIT: It now support negative numbers as well. Is String A Number.vi Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.