Regex to check the string contains only letter and numbers but not only numbers?

Here are the components of the regex we're going to use.

Here are the components of the regex we're going to use: ^ and $ are the beginning and end of the string anchors respectively \d matches a digit a-zA-Z matches a letter a-zA-Z\d matches a letter or a digit * is "zero-or-more" repetition With these, we can now compose the regex we need (see on rubular. Com): ^\d*a-zA-Za-zA-Z\d*$ Here's an explanation of the pattern: from the beginning... till the end | | ^\d*a-zA-Za-zA-Z\d*$ \_/\______/\_________/ The 3 parts are: Maybe some digits as a prefix... But then definitely a letter! And then maybe some digits and letters as a suffix References regular-expressions.Info/Character Class, Anchors, and Repetition.

2 +1 for lack of +. – Kobi Jul 29 '10 at 10:41 1 (I'm sure @Kobi already knows but let's make it explicit) ...which is a good thing! Omitting the + for the second part simplifies the backtracking process in case there's no match.

This pattern will not enter the second part unless all digits-only prefix (no more, no less) is matched by the first part, and if it enters the third part, then the second part does not backtrack (since it's not even repeatable). Both repetitions can also be made possessive for further optimization if necessary. – polygenelubricants Jul 29 '10 at 10:59 +1 for actually explaining the answer rather than just giving it up.

– Alex Larzelere Jul 29 '10 at 11:43 +1 because this answer taught me something. – afuzzyllama Jul 29 '10 at 15:55.

This should do it: ^0-9*a-zA-Z+a-zA-Z0-9*$ This requires at least one character of a-zA-Z.

Ah, saw yours as soon as I finished posting. – Carl Jul 29 '10 at 10:35 doesn't work! – iamserious Jul 29 '10 at 10:36 @iamserious - rubular.Com/r/0sCHEd54Hk – Kobi Jul 29 '10 at 10:38 @iamserious Are you really serious?

It looks okay to me. – Amarghosh Jul 29 '10 at 10:39 wait a minute, I was checking using this: gskinner.Com/RegExr/?2rsb2 and obviously dint match anything there! I'm embarrassed now!

– iamserious Jul 29 '10 at 10:46.

– Carl Jul 29 '10 at 10:33 @Carl: Yes, it does. If that happens, backtracking will be required to find a match that fulfills the whole regular expression. – Gumbo Jul 29 '10 at 10:35.

Instead of using a regular expression, you can also use the ctype_*() functions: var_dump(ctype_alnum('letters') &&! Ctype_digit('letters')); // bool(true) var_dump(ctype_alnum('0123chars') &&! Ctype_digit('0123chars')); // bool(true) var_dump(ctype_alnum('1324') &&!

Ctype_digit('1324')); // bool(false) var_dump(ctype_alnum('xcvxxc%$#') &&! Ctype_digit('xcvxxc%$#')); // bool(false) But if you want a regular expression, you can use this: var_dump(preg_match('/^a-z0-9*a-z+a-z0-9*$/i', $input)).

I think ctype will be perfect here – iamserious Jul 29 '10 at 10:39.

Personally (I hate regex and find them generally to be hard to maintain), I'd do it in two steps. Is it all alphanumeric? Is there at least one letter?

I hate the code my old colleague left behind and find it generally hard to maintain. On a relevant note, you are correct, but the answer would have looked much better with some code. – Kobi Jul 29 '10 at 11:22.

(?=. *a-z)a-zA-Z0-9+$ (?=. *a-z) positive lookahead to make sure that there is at least one letter in the string (but doesn't consume any characters - gives up the matched characters as soon as it returns (in this case, as soon as it finds a letter)).

A-zA-Z0-9+ make sure string contains only alphanumeric characters. ^ and $ are start and end of string delimiters.

0-9*a-zA-Z+0-9a-zA-Z*$ translated: from the beginning, match 0 or more numbers, then match at least one letter, then match zero or more letters or numbers until the end.

1 That allows the empty string. – MvanGeest Jul 29 '10 at 10:26 The replace the last asterisk with a plus-sign: (0-9*a-zA-Z+)+. – faileN Jul 29 '10 at 10:31 this matches purely alphabets.. not alphanumeric – iamserious Jul 29 '10 at 10:36 thanks you @all, I have made the change – Sadat Jul 29 '10 at 11:36.

This should work ^(a-zA-Z0-9*)(a-zA-Z+)(a-zA-Z0-9*)$ EDIT sry got the * and the + messed up.

Does not allow single letter and does allow numbers only – unbeli Jul 29 '10 at 10:32 You need to switch the * and + signs. – BoltClock? Jul 29 '10 at 10:32 It won't work... – Guillaume Lebourgeois Jul 29 '10 at 10:38 sry I got the * and +, well now it should do it – user338128 Jul 29 '10 at 11:33.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions