regex replace all non numerics with blank???

  • Thread starter Thread starter rob merritt
  • Start date Start date
R

rob merritt

Hi I have a simple problem
hopefully. how would I replace all no numerics using <string name>.regex

if if I have a string like


123456.309.123456
or
123456-307-123456
or
123456 307 123456

all need to be nomalized to

123456307123456

so if

bn="123456-307-123456"
bn=bn.regex([0-9])

any ideas
 
Hello,

your idea was right but you forgot the leading ^ which means a negation of
your expression. So [^0-9]. But you can use the following, too:

string number = "123.456.344";
number = System.Text.RegularExpressions.Regex.Replace(number, @"\D", "");
int real_number = Convert.ToInt16(number);

\D means all non-numeric chars are replaced by "".

Hope it helps.

Matthias
 
Back
Top