Link to home
Start Free TrialLog in
Avatar of jimbona27
jimbona27Flag for United Kingdom of Great Britain and Northern Ireland

asked on

alphanumeric regular expression needed

hi EE,
Can I have a regular expression example in C# (ASP.NET) that checks a string to be a alphanumeric please?
Thanks,
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Use a pattern like this:

^[A-Za-z0-9]+$
(^-?[a-zA-Z0-9]*$)

this should do it
jimbona27,

Please clarify what you mean by "alphanumeric".

For example, my pattern will reject the following:

<zero length string>
-
-1
-a
-234xyz

brad2575's pattern will accept all of them...

Patrick
(^-?[a-zA-Z0-9]*$) should be the one.

For more about regular expressions, check out:
http://www.regular-expressions.info

It contains a similar example as well. Check out:
http://www.regular-expressions.info/numericramges.html

Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, for my last post, a slight modification would be needed to validate that the string was alpha-numeric:

[^a-zA-Z0-9]+
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ErrorMessage="Please enter an alpha-numeric string" ControlToValidate="TextBox1" 
    ValidationExpression="[^a-zA-Z0-9]+"></asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Open in new window

Hi jimbona27,
You can also use "\w+" to check that all characters are alphanumeric.

This is a tutorial that I found very helpful:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx

You can also download the expresso regex application which is based on .NET technology (i.e. like c#).

Hope this helps.
P.
use @"\w+" to string escape it.
P.
\w

also matches underscore, which the OP may not want  :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My previous post contains error in some input samples.

Try the below expression:

^\\w+[^_]$

Hope this will work.



-Shahan
^\\w+[^_]$

will match:

    abc_def
    _1
@ kaufmed

You posted this regular expression & ASP.NET code for its use
[^a-zA-Z0-9]+


  1) ASDV09asd
       this is treated as non-alphanumeric. I could not understand ????

I was testing the patterns from here:

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
@Shahan_Developer

I was thinking at the time that RegularExpressionValidator displayed its error message if the pattern was successful, not if it failed. It seems the latter is correct--good catch Shahan_Developer. The first post, 30658606, would be the proper usage.

@ kaufmed

In your post, 30658606, your approach seems to be correct but still getting unexpected result.
In your post, 30658606 you are right mistake is from my side.
Thanks kaufmed,
I didn't know that the underscore was treated as part of the alphanumeric type, anyhow, I just wanted to post the corrected other way for matching:

@"^[\w-[_]]+$"

This will work too.
P.