How can I check a string to only alphanumeric characters in it?

How can I check a string using regular expressions to allow alphanumeric characters?

(I also do not want to allow any spaces).

+101
c # regex
Jun 25 '09 at 23:45
source share
8 answers

Use the following expression:

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

i.e:

 using System.Text.RegularExpressions; Regex r = new Regex("^[a-zA-Z0-9]*$"); if (r.IsMatch(SomeString)) { ... } 
+163
Jun 25 '09 at 23:47
source share

In .NET 4.0, you can use LINQ:

 if (yourText.All(char.IsLetterOrDigit)) { //just letters and digits. } 

yourText.All will stop execution and return false the first time that char.IsLetterOrDigit reports false char.IsLetterOrDigit since then the All contract cannot be executed.

The note! this answer does not check alphanumeric characters (usually AZ, az and 0-9). This answer allows local characters like Γ₯Àâ .

Update 2018-01-29

The syntax above only works when you use a single method that has one argument of the correct type (in this case, char ).

To use several conditions, you need to write like this:

 if (yourText.All(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x))) { } 
+184
May 20 '13 at 1:04
source share

You can do this easily with an extension function rather than a regular expression ...

 public static bool IsAlphaNum(this string str) { if (string.IsNullOrEmpty(str)) return false; for (int i = 0; i < str.Length; i++) { if (!(char.IsLetter(str[i])) && (!(char.IsNumber(str[i])))) return false; } return true; } 

For comment:) ...

 public static bool IsAlphaNum(this string str) { if (string.IsNullOrEmpty(str)) return false; return (str.ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c))); } 
+32
Jun 25 '09 at 23:52
source share

While I think that a regex-based solution is likely to be the way I would go, I will be tempted to encapsulate it into a type.

 public class AlphaNumericString { public AlphaNumericString(string s) { Regex r = new Regex("^[a-zA-Z0-9]*$"); if (r.IsMatch(s)) { value = s; } else { throw new ArgumentException("Only alphanumeric characters may be used"); } } private string value; static public implicit operator string(AlphaNumericString s) { return s.value; } } 

Now that you need a verified string, you can have a method signature that requires an AlphaNumericString and know that if you get it, it is valid (except for zeros). If someone tries to pass in an unaudited line, he will generate a compiler error.

If you're interested, you can get fancier and implement all equality operators or explicit casts to AlphaNumericString from a simple string.

+17
Jun 26 '09 at 3:04
source share

I needed to check AZ, az, 0-9; without regex (although the OP requests a regex).

Mixing the various answers and comments here and discussing https://stackoverflow.com/a/166268/2128/ , these are tests for a letter or number, avoiding other letters of the language and avoiding other numbers, such as fraction characters.

 if (!String.IsNullOrEmpty(testString) && testString.All(c => Char.IsLetterOrDigit(c) && (c < 128))) { // Alphanumeric. } 
+8
Jun 10
source share

^\w+$ will allow a-zA-Z0-9_

Use ^[a-zA-Z0-9]+$ to disable underlining.

Note that both of them require that the string is not empty. Using * instead of + allows blank lines.

+3
Jun 25 '09 at 23:48
source share

To check if a string is a combination of letters and numbers, you can rewrite @jgauffin's answer as follows using .NET 4.0 and LINQ:

 if(!string.IsNullOrWhiteSpace(yourText) && yourText.Any(char.IsLetter) && yourText.Any(char.IsDigit)) { // do something here } 
+2
Feb 25 '16 at 9:23
source share

I advise you not to depend on ready-made and embedded code in the .NET framework, try to bring up a new solution .. this is what I do ..

 public bool isAlphaNumeric(string N) { bool YesNumeric = false; bool YesAlpha = false; bool BothStatus = false; for (int i = 0; i < N.Length; i++) { if (char.IsLetter(N[i]) ) YesAlpha=true; if (char.IsNumber(N[i])) YesNumeric = true; } if (YesAlpha==true && YesNumeric==true) { BothStatus = true; } else { BothStatus = false; } return BothStatus; } 
-7
May 6 '16 at 1:13 p.m.
source share



All Articles