29

I have the following regex ^[a-zA-Z0-9]+$ which would allow alpha numeric characters. The problem here is that if I enter only numeric character like "897687", then the regex still matches. I don't want that to happen. There should be at least one text character and it should start with a text character. For example like "a343" or "a98bder" or "a4544fgf343"

It would be great if you could help me to improve my regex for this.

1
  • 2
    As an aside, with "there should be at least one alphabet and it should start with an alphabet", the latter means that the former will always be true. So the requirement is "it should start with an alphabet". Now to go upvote Rowland since he understood this quite well :-) Commented Nov 15, 2011 at 9:03

7 Answers 7

52

Sounds like you want:

^[a-zA-Z][a-zA-Z0-9]*$

EXPLANATION

^ asserts position at the start of a line

Match a single character present in the list below [a-zA-Z]

» a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)

» A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)

Match a single character present in the list below [a-zA-Z0-9]*

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)

A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)

0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

$ asserts position at the end of a line

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me understand, I always see "zero and unlimited", does that mean it'll return true even with zero matches? like saying "I'll give you zero or unlimited cookies" kinda sounds like I'm not getting any cookies :)
Exactly that Dan... you could use + for 1 or more to make sure you get at least one cookie ;)
21

Just in case that the ASCII characters are at some point not enough, here the Unicode version:

^\p{L}[\p{L}\p{N}]*$

\p{L} is any Unicode code point that has the property letter ==> Any letter from any language (that is in Unicode)

\p{N} is any Unicode code point that has the property number ==> Any number character from any language (that is in Unicode)

1 Comment

This should be the accepted answer, it's much more robust if the input is not necessarily ANSI.
6

This function will return true or false based on whether the Regular Expression is matched or not,

   public static Boolean isAlphaNumeric(string strToCheck)
    {
        Regex rg = new Regex(@"^[a-zA-Z0-9\s,]*$");
        return rg.IsMatch(strToCheck);
    }

Comments

5
^[a-zA-Z][a-zA-Z0-9]*$

Should do the trick!

Alternatively, if you wish to include all alphanumeric plus an underscore you can use:

^[a-zA-Z][\w]*$

2 Comments

Whitespace is definitely not included in \w. In .net it is [\p{L}\p{N}_] it includes all Unicode letters, numbers and (at least) the underscore.
@stema Sorry typo, my head said underscore, but my fingers typed whitespace. Good spot, thanks.
3

Or slightly less verbose than the accepted answer:

^[a-zA-Z][\w]*$

C# regex has character class indicator "\w" for alphanumeric characters, but does not have a character class for just alphabetic (no digits), hence you have to specify class set [a-zA-Z] manually.

1 Comment

As described previously, [\w] includes the underscore character which is not asked for in the original post and is not equivalent to the accepted answer. msdn.microsoft.com/en-us/library/vstudio/…
3

This is the best solution to check alphanumeric,

  • if it is only string - "Error".
  • if it is only integer - "Error".
  • if it is alphanumeric -"Success".

    if (System.Text.RegularExpressions.Regex.IsMatch(txt.Text, @"[a-zA-Z]") && 
        System.Text.RegularExpressions.Regex.IsMatch(txt.Text, @"[0-9]")      
    {
        // Success - It is alphanumric
    }
    else   
    {
        // Error - It is not alphanumric
    }
    

Comments

1

Because I don't see it mentioned in the other answers – you can set an in-line flag to make the pattern case-insensitive:

(?i)^[a-z][a-z0-9]+$

And, regarding _ being included in \w, you can exclude certain characters from character ranges:

(?i)^[a-z][\w-[_]]+$

However, this is harder to read and it's not like it makes the pattern any shorter.

More importantly, if the regex options don't include the ECMAScript enum (it doesn't by default), \w will match letters beyond a-z & A-Z – such as accented letters, letters in other languages, etc.

In any case, it's always nice to know about all the options available to you.


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.