03-22-2011 08:58 AM
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
Solved! Go to Solution.
03-22-2011 09:09 AM
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.
03-22-2011 09:12 AM
You can use a regular expression to match the pattern.
03-22-2011 09:43 AM - edited 03-22-2011 09:47 AM
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
03-22-2011 09:51 AM - edited 03-22-2011 09:53 AM
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.
03-22-2011 09:52 AM
@_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.
03-22-2011 10:05 AM
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
03-22-2011 11:17 AM