Jump to content

String Checker


Recommended Posts

Does anyone know of a string VI that checks a string to see if it is a valid decimal number before sending the string to a convert VI? The string could have one of 4 possibilities for the first character (+, -, Number, Decimal Pt.), and can then have either numbers or a decimal pt. after. It must not have more than one decimal Pt., or Plus/Minus sign. Just thought I would check before I start doing my own. I am using LV 8.2. It would be nice if the string to number conversions had some error output.

Thanks,

Sparky

Link to comment

QUOTE(Sparky @ Sep 6 2007, 10:25 AM)

There are actually a couple ways to do this. You can use the Match Pattern function, or you could do a full-blown PCRE with the Regular Expression function (that I love so very much).

In this case, a Match Pattern is probably good enough. I think this snippet gets you most of what you want:

http://forums.lavag.org/index.php?act=attach&type=post&id=6869

To break it down...

  • ^ anchor the match to the beginning of the string (require the number to occur at the front of the string)
  • [+-]? match the '+' or '-' character 0 or 1 times (so strings without a + or - still match)
  • [0-9]* match any digits 0 or more times (so strings that don't have digits in front of the decimal still match)
  • \.? match the literal decimal point ('.') 0 or 1 times (so strings that don't have the decimal at all still match)
  • [0-9]* match any digits 0 or more times (so strings that don't have digits after the decimal still match)
  • $ anchor the match to the end of the string (so any extra characters beyond the number cause a failed match)

Now, it turns out this example is not without its problems. For instance, it will still match:

  • An empty string (change the >=0 to a strict >0 to fix that)
  • Various non-numeric strings like "+" or "." or "+."

The second item is kind of tough to deal with, although you could run it through a pre-validation step with a regex like [0-9]? to make sure the string contains at least one digit. You could also probably handle it with a cleverly constructed regex and the full-blown Regular Expression function, but that's left as an exercise to the reader ;).

Link to comment

QUOTE(Justin Goeres @ Sep 6 2007, 11:02 AM)

That example will also incorrectly match the string ".0.3"

Hehe... what's wrong with that? :P

Dunno, I can't think of a reason they were looking for an initial period at the front of the number... ah well, so much for the examples :)

Link to comment

QUOTE(orko @ Sep 6 2007, 02:13 PM)

Could have a decimal point as the first char. if the operator enters .985 for example instead of 0.985. I don't think I can guarantee that the operator will not just enter the decimal point first. I guess I could force a zero as the first char if the operator just entered a decimal point. I would prefer to allow him/her to do it either way though. Thanks for all the good comments.

Sparky

QUOTE(wevanarsdale @ Sep 6 2007, 01:51 PM)

Sparky: I'd use the
Fract/Exp String to Number
function to convert a string or array of strings. This conversion is not sensitive to characters subsequent to the numeric part. You can set the default value to NaN (assuming this value does not appear in your data) and test for a conversion error. You can convert back using the
Number to Fractional String
or similar function to remove any trailing non-numeric characters. This approach is useful for reformatting numeric string arrays.

I think the "Fract/Exp String to Number needs the leading zero, and won't work if the first char. is just a decimal point.

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.