RegEx: Remove all characters except [0-9]+

G

Gina_Marano

Good day all,

I am in need of help to create a regular expression that will remove
all characters that aren't 0-9. This is to cleanup junk phone numbers
and social security/personal numbers. I want to be extreme and remove
any character because you never know what they user entered.

For example:

123_123-123 should return as 123123123
(233 555-1212 shoul return as 2335551212

I don't want any spaces or anything just the numbers.

Thanks for any help.

~Gina_M~
 
G

Gina_Marano

Hey All,

This appear to work great!

cleanval = Regex.Replace(dirtyval, @"[^0-9]", "").Trim(".".ToCharArray
()); //found in another post and modified a little

~Gina_M~
 
G

Göran Andersson

Gina_Marano said:
Hey All,

This appear to work great!

cleanval = Regex.Replace(dirtyval, @"[^0-9]", "").Trim(".".ToCharArray
()); //found in another post and modified a little

~Gina_M~

You don't have to trim anything. There are no periods in the string
after replacing.

Put a + in the regular expression, that way you replace chunks of
characters instead of replacing one character at a time.

You can use the character class \D instead of [^0-9].

Use the string.Empty constant isntead of an empty literal string. That
way it's clearer that you actually intended to use an empty string and
not just forgot to put anything in the string.

cleanval = Regex.Replace(dirtyval, @"\D+", string.Empty);
 
K

KH

A regex is a bit overkill for this; how about:

string input = "..."; // unknown input string

StringBuilder sb = new StringBuilder(input.Length);

foreach (char ch in input)
{
if (Char.IsDigit(ch)) // or if (ch >= '0' && ch <= '9')
{
sb.Append(ch);
}
}

return sb.ToString();


HTH
 
J

Jeff Johnson

A regex is a bit overkill for this; how about:

If you're familiar with regular expressions then I think that the one-liner
Replace() is far cleaner than the manual method you posted. Unless you're
going to be doing this in a loop where performance matters, I'd go with the
regex.
 
G

Göran Andersson

KH said:
A regex is a bit overkill for this

Well, we could really go for the overkill... LINQ is used for everything
these days...

return new string((from char c in input where char.IsDigit(c) select
c).ToArray());

;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top