tags:

views:

5508

answers:

4

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";
+2  A: 

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.

zigdon
you're right, that's starts with (which i knew, but it's late)
John Sheehan
+1  A: 

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])+"

JaredPar
Wouldn't that replace all the alphanumerics and spaces with the empty string?
zigdon
Regarding #2, the quantifier doesn't really matter as he wants to replace all non-matching characters in the string rather than just a single run of them, which requires a global replace (.../g in Perl, not sure of the C# syntax), with or without the */+.
Dave Sherohman
A: 

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

Windows programmer
Yeah, I want it inside. Match anything that isn't these character ranges
John Sheehan
Oh, where you said "I want" I thought you meant you wanted a regular expression to match that. You meant you want the result of Replace to be that. So you want the regular expression to not match that. My brain hurts.
Windows programmer
+1  A: 

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

John Sheehan
Your regex will only match strings which do not contain alpha numerics, numbers or spaces. The ^ at the start of a [] means "not anything inside here"
JaredPar
That's exactly what I want. In the Regex.Replace, I want to match anything that's NOT a letter, number or space.
John Sheehan
Ah okay, clearer now.
JaredPar