[c#] Regex to match alphanumeric and spaces

What am I doing wrong here?

string q = "john s!";
string clean = Regex.Replace(q, @"([^a-zA-Z0-9]|^\s)", string.Empty);
// clean == "johns". I want "john s";

This question is related to c# regex

The answer is


just a FYI

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

would actually be better like

string clean = Regex.Replace(q, @"[^\w\s]", string.Empty);

This:

string clean = Regex.Replace(dirty, "[^a-zA-Z0-9\x20]", String.Empty);

\x20 is ascii hex for 'space' character

you can add more individual characters that you want to be allowed. If you want for example "?" to be ok in the return string add \x3f.


I suspect ^ doesn't work the way you think it does outside of a character class.

What you're telling it to do is replace everything that isn't an alphanumeric with an empty string, OR any leading space. I think what you mean to say is that spaces are ok to not replace - try moving the \s into the [] class.


The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.


The following regex is for space inclusion in textbox.

Regex r = new Regex("^[a-zA-Z\\s]+");
r.IsMatch(textbox1.text);

This works fine for me.


There appear to be two problems.

  1. You're using the ^ outside a [] which matches the start of the line
  2. You're not using a * or + which means you will only match a single character.

I think you want the following regex @"([^a-zA-Z0-9\s])+"


I got it:

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

Didn't know you could put \s in the brackets


The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.


I suspect ^ doesn't work the way you think it does outside of a character class.

What you're telling it to do is replace everything that isn't an alphanumeric with an empty string, OR any leading space. I think what you mean to say is that spaces are ok to not replace - try moving the \s into the [] class.


There appear to be two problems.

  1. You're using the ^ outside a [] which matches the start of the line
  2. You're not using a * or + which means you will only match a single character.

I think you want the following regex @"([^a-zA-Z0-9\s])+"


The following regex is for space inclusion in textbox.

Regex r = new Regex("^[a-zA-Z\\s]+");
r.IsMatch(textbox1.text);

This works fine for me.


This:

string clean = Regex.Replace(dirty, "[^a-zA-Z0-9\x20]", String.Empty);

\x20 is ascii hex for 'space' character

you can add more individual characters that you want to be allowed. If you want for example "?" to be ok in the return string add \x3f.


bottom regex with space, supports all keyboard letters from different culture

 string input = "78-selim güzel667.,?";
 Regex regex = new Regex(@"[^\w\x20]|[\d]");
 var result= regex.Replace(input,"");
 //selim güzel

I got it:

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

Didn't know you could put \s in the brackets


I got it:

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

Didn't know you could put \s in the brackets


The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.


bottom regex with space, supports all keyboard letters from different culture

 string input = "78-selim güzel667.,?";
 Regex regex = new Regex(@"[^\w\x20]|[\d]");
 var result= regex.Replace(input,"");
 //selim güzel

The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.


I suspect ^ doesn't work the way you think it does outside of a character class.

What you're telling it to do is replace everything that isn't an alphanumeric with an empty string, OR any leading space. I think what you mean to say is that spaces are ok to not replace - try moving the \s into the [] class.