regular expression pattern needed

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I need to strip the ()- and spaces from a telephone number. What is the
regular express pattern for this? "\(\)-"?

thanks
 
that works. this too:

'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "[^0-9]","")

Brian Davis said:
Try this:

PhoneNumber = Regex.Replace(PhoneNumber, "[()-]","")

Brian Davis
www.knowdotnet.com


Craig Buchanan said:
I need to strip the ()- and spaces from a telephone number. What is the
regular express pattern for this? "\(\)-"?

thanks
 
Craig,
Which can be shorted to:
'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "\D","")

However, yours & mine will clobber phone numbers that are words:

1-800-FOR-HELP

Hope this helps
Jay

Craig Buchanan said:
that works. this too:

'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "[^0-9]","")

Brian Davis said:
Try this:

PhoneNumber = Regex.Replace(PhoneNumber, "[()-]","")

Brian Davis
www.knowdotnet.com


Craig Buchanan said:
I need to strip the ()- and spaces from a telephone number. What is the
regular express pattern for this? "\(\)-"?

thanks
 
Jay-

What does the \D mean?

Thanks,

Craig

Jay B. Harlow said:
Craig,
Which can be shorted to:
'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "\D","")

However, yours & mine will clobber phone numbers that are words:

1-800-FOR-HELP

Hope this helps
Jay

Craig Buchanan said:
that works. this too:

'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "[^0-9]","")

Brian Davis said:
Try this:

PhoneNumber = Regex.Replace(PhoneNumber, "[()-]","")

Brian Davis
www.knowdotnet.com


I need to strip the ()- and spaces from a telephone number. What is the
regular express pattern for this? "\(\)-"?

thanks
 
Craig,
What does the \D mean?
"\D" effectively means "[^0-9]"

Per:
http://msdn.microsoft.com/library/d...en-us/cpgenref/html/cpconCharacterClasses.asp

\D : Matches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for
non-Unicode, ECMAScript behavior.

Most of the time I prefer the character codes \d over [0-9] or \D over
[^0-9] as they are more internationalized (they will work for any Unicode
numeric value, not just the ANSI 0 to 9 characters. Also they tend to be
shorter ;-)

Hope this helps
Jay

Craig Buchanan said:
Jay-

What does the \D mean?

Thanks,

Craig

Jay B. Harlow said:
Craig,
Which can be shorted to:
'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "\D","")

However, yours & mine will clobber phone numbers that are words:

1-800-FOR-HELP

Hope this helps
Jay

Craig Buchanan said:
that works. this too:

'strip anything that isn't a number
PhoneNumber = Regex.Replace(PhoneNumber, "[^0-9]","")


Try this:

PhoneNumber = Regex.Replace(PhoneNumber, "[()-]","")

Brian Davis
www.knowdotnet.com


I need to strip the ()- and spaces from a telephone number. What
is
the
regular express pattern for this? "\(\)-"?

thanks
 
Back
Top