LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Verifying that a string contains only number data

Solved!
Go to solution

Hello,

 

I am trying to find an elegant way to test an input "serial number" string of the format A00000000 (where A can be any letter and 0 can be any number).  My first thought was to verify the following traits:

 

1) total string length = 9.

2) using "Decimal String to Number.vi", result = 0 (indicating that the string contains non-numbers).

3) using "Decimal String to Number.vi" with an offset of 1, result is a number.

 

Unfortunately, this does not differentiate between the following strings:

 

A00005123     Valid    

A5123             Invalid   

A5123B123     Invalid   

A005123BC     Invalid   

 

Has anyone done this recently?  Is there a way to parse by non-specific tokens (e.g. separate into groups of letters, numbers, or other?)  Is there a comprehensive way to do this without breaking the string down into 9 individual elements and doing a 26 element comparison for the letter and 10 element comparisons for each number?

 

Thanks,

Sean

0 Kudos
Message 1 of 8
(7,802 Views)

Search and Replace String can be configured for regular expressions (right-click it).  I'm no "regexpert" but I know there are people here that can do your test in their sleep.

 

I'll try to figure out the proper search string but I'll bet someone beats me to the best answer.  Look in the LV help for 'Regular Expression' to get more info on your own.

 

 

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 2 of 8
(7,797 Views)
Solution
Accepted by topic author Sean.C

You can use a regular expression to match the pattern.

 

Match Serial Number.png



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 3 of 8
(7,795 Views)

I've been trying to get the hang of regexs, so as I understand it, Mark's regex means the following:

 

[A|Z]  - look for one capital letter that is A or Z ( I was expecting [A-Z] to select anything in the range A to Z  ??)

[0-9] - look for any number

{8,8} - Repeat the number search no less than 8 times and no more than 8 times.

 

And so, if 'offset past match' equals 9, the string must be the correct format (as long as the total string length is not more than 9 characters).

 

Have I got that right?

Thanks,

Ian

0 Kudos
Message 4 of 8
(7,783 Views)

I believe it should be [A-Z] with a dash since the OP needs to match any letter.

 

I think he's going to need to also flag on bad user input if the string is too long.  PCRE truncates the string properly but it doesn't indicate that problem.  OP could always AND in a test for proper string length I guess.

 

EDIT:  Ian already caught it. Smiley Sad

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 5 of 8
(7,772 Views)

 


@_Ian_ wrote:

I've been trying to get the hang of regexs, so as I understand it, Mark's regex means the following:

 

[A|Z]  - look for one capital letter that is A or Z ( I was expecting [A-Z] to select anything in the range A to Z  ??)

[0-9] - look for any number

{8,8} - Repeat the number search no less than 8 times and no more than 8 times.

 

And so, if 'offset past match' equals 9, the string must be the correct format (as long as the total string length is not more than 9 characters).

 

Have I got that right?

Thanks,

Ian


Yes, that is the correct way to interpret the regular expression. My original post should have been [A-Z]. The "or" was a typo. The {} expression is specifying the number of times the preceeding expression repeats. This can follow something longer than a single character.

 



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 6 of 8
(7,771 Views)

Thanks Mark, (and everyone else)

 

I new there had to be a simple way to do this, and you knew it.  🙂

 

The followup posts are correct about the sample string given.  [A|Z] is A or Z.  I ended up using the following string:

 

[A-Z][0-9]{8}

 

This passes the entire string if it is correct.  I then did an equal comparison between the original string and the match string to ensure that nothing was on either side of it.  (I also tried some invalid strings and it catches those as well).

 

Thanks again everyone,

Sean

0 Kudos
Message 7 of 8
(7,762 Views)

Instead of using the offset past match use the word boundary \b in your regex. The regex will be [A-Z][0-9]{8}\b

 

It will match A12345678 but not A123456789.

 

 

Ben64

Message 8 of 8
(7,734 Views)