Jump to content

Is Numeric String?


Recommended Posts

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

Link to comment

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.

Link to comment

QUOTE (jlokanis @ Oct 8 2008, 10:56 AM)

Well it's pretty easy to write in LabVIEW without regular expressions

post-1764-1223491077.png?width=400

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

Link to comment

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:

Link to comment

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

Link to comment

^(?:\d+(?:\.\d*)?|\.\d+)(?:\d+)?$

this will also recognize .8

I am pretty happy that finally LV supports regexes, makes life much easier. :D

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

Link to comment

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

Link to comment

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:

Link to comment
  • 5 months later...

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

Link to comment
  • 5 years later...
  • 5 months later...

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?

Link to comment

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 :P ). 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". :D

Link to comment

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.

Link to comment

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.

  • Like 1
Link to comment

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.

Link to comment

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

Link to comment

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:

post-28303-0-91888500-1425939031.png

  • Like 2
Link to comment
  • 2 weeks later...

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

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.