This article (http://www.experts-exchange.com/Programming/Q_21668801.html?query=format+phone+number+with+regex.replace&clearTAFilter=true
) showed me how to use Regex.Replace to format a string as a phonenumber, which for the most part is great. However, if there is no extension in the phone number I don't want there to be a trailing hiphen. How do I adjust the regular expression so that the last hipen between the phone number suffix and the extension is only used if there is an extension present in the phone number?
Here's what I'm using:
string phoneNumber = "1234567890"
ResultString = Regex.Replace(phoneNumber,
(\\d*)$", "($1) $2-$3-$4");
Output: "(123) 456-7890-"
I want the output to look like "(123) 456-7890"
or like "(123) 456-7890-1234" if an extesion exists.
Thanks in advance