LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unique regular expression to check if a string contains letters and numbers

Hi all,

How can I verify if a string contains numbers AND letters using a regular expression only?

I can do that with using 3 different expressions ([0-9],[a-z],[A-Z]) but is there a unique regular expression to do that?

 

Thanks all

0 Kudos
Message 1 of 13
(11,882 Views)

@AC_85 wrote:

Hi all,

How can I verify if a string contains numbers AND letters using a regular expression only?

I can do that with using 3 different expressions ([0-9],[a-z],[A-Z]) but is there a unique regular expression to do that?

 

Thanks all


You can combine the classes into a single one [0-9a-zA-Z] or use the posix class [[:alnum:]].  To verify you simply match the negative class [^0-9a-zA-Z] or [^[:alnum:]], a non-empty match means that a non-alphanumeric was in the string.

0 Kudos
Message 2 of 13
(11,873 Views)

!1-1.png

two ways to skin that cat.  


"Should be" isn't "Is" -Jay
0 Kudos
Message 3 of 13
(11,850 Views)

It looks like the previous answers will match a string that contains alpha or num, not alpha and num only.

 

snip.png

 

snip2.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 4 of 13
(11,837 Views)

@jcarmody wrote:

It looks like the previous answers will match a string that contains alpha or num, not alpha and num only.

 

 


Jim, Thanks for pointing out I missed the req.

 

I'd still go with the lex class over the regex just for readability (unless you are a regex guru)

!1-1.png

"True if: A decimal digit Character exists And a alphabetic char exists And all chars are either Alpha or numeric" 

 


"Should be" isn't "Is" -Jay
Message 5 of 13
(11,826 Views)

@JÞB wrote:

 

I'd still go with the lex class over the regex just for readability (unless you are a regex guru)


I'm waiting for a guru to swing by and replace my regex with something much more concise.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 6 of 13
(11,819 Views)

Missed the requirements:

 

single regex:

 

^([[:alpha:]]+[[:digit:]]+|[[:digit:]]+[[:alpha:]])[[:alnum:]]*$

 

You either have 1 or more digits followed by 1 or more letters or 1 or more letters followed by 1 or more digits. Once that is out of the way, the rest of the string must be all alphanumerics.  The ^ and $ make sure the whole string is included in the match.

 

(I have not tested this at all, just typed it up hopefully I got all the brackets in the right place).

0 Kudos
Message 7 of 13
(11,813 Views)

@Darin.K wrote:

Missed the requirements:

 

single regex:

 

^([[:alpha:]]+[[:digit:]]+|[[:digit:]]+[[:alpha:]])[[:alnum:]]*$

 

You either have 1 or more digits followed by 1 or more letters or 1 or more letters followed by 1 or more digits. Once that is out of the way, the rest of the string must be all alphanumerics.  The ^ and $ make sure the whole string is included in the match.

 

(I have not tested this at all, just typed it up hopefully I got all the brackets in the right place).


I think you just made my point.  TWICE.Smiley Very Happy  While the lex class would be much more readable as a ring.... I know all my brackets are in the correct places and don't need to hope.


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 13
(11,797 Views)

If you are keeping score:

 

1.  Bracket balancing is only a problem on my phone, in LV it is a easy to debug error.

2.  Using my regex helper means I do not even have to worry about it or remembering the exact syntax of any of the posix classes.

3.  I know precisely what happens with unicode fonts and languages with letters outside of A-Z (and I think I know precisely what does not happen with the lexical class function)

4.  Calculate how much time it takes to modify each version when you decide that radix points (. or ,) should be allowed, and then decide that negative numbers should be allowed.

Message 9 of 13
(11,790 Views)

I was not arguing the power of a regex.. I do use them often.  Not nearly as masterfully as I wish. but, that is what test cases and regex helpers are for!  In this case, a simpler to understand solution presented itself.  I like easy to understand code.  No love lost- the regex you showed probably would pass my helper.  (I did not test it either but, Darin can usually be trusted to type)

 

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 13
(11,782 Views)