06-19-2014 10:53 AM
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
06-19-2014 11:03 AM
@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.
06-19-2014 01:32 PM - edited 06-19-2014 01:34 PM
two ways to skin that cat.
06-19-2014 02:12 PM
06-19-2014 02:50 PM
@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)
"True if: A decimal digit Character exists And a alphabetic char exists And all chars are either Alpha or numeric"
06-19-2014 03:09 PM
@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.
06-19-2014 03:39 PM
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).
06-19-2014 07:01 PM
@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. 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.
06-19-2014 07:43 PM
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.
06-19-2014 08:35 PM
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)