Reputation: 275
I need to format my telephone numbers in a specific way. Unfortunately business rules prohibit me from doing this up front. (separate input boxes etc..)
The format needs to be +1-xxx-xxx-xxxx
where the "+1" is constant. (We don't do business internationally)
Here is my regex pattern to test the input:
"\\D*([2-9]\\d{2})(\\D*)([2-9]\\d{2})(\\D*)(\\d{4})\\D*"
(which I stole from somewhere else)
Then I perform a regex.Replace() like so:
regex.Replace(telephoneNumber, "+1-$1-$3-$5"); **THIS IS WHERE IT BLOWS UP**
If my telephone number already has the "+1" in the string, it prepends another so that I get +1-+1-xxx-xxx-xxxx
Can someone please help?
Upvotes: 0
Views: 1864
Reputation: 700720
You can add (?:\+1\D*)?
to catch an optional prefix before the number. As it's caught it will be replaced if it's there.
You don't need to use \D*
before and after the number. As they are optional, they don't change anything.
You don't need to capture the parts that you won't use, that makes it easier to see what ends up in the replacement.
str = Regex.Replace(str, @"(?:\+1\D*)?([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})", "+1-$1-$2-$3");
You might consider using something more specific than \D*
for the separators though, for example [\- /]?
. With a too non-specific pattern you risk catching something that's not a phone number, for example changing "I have 234 cats, 528 dogs and 4509 horses."
into "I have +1-234-528-4509 horses."
.
str = Regex.Replace(str, @"(?:\+1[\- /]?)?([2-9]\d{2})[\- /]?([2-9]\d{2})[\- /]?(\d{4})", "+1-$1-$2-$3");
Upvotes: 2
Reputation: 627344
You can use the following regex
\D*(\+1-)?([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})\D*
And the replacement string:
$1$2-$3-$4
Here is a demo
This is a kind of an adjustment of the regex you had. If you need to match the whole numbers, I'd use
(\+1-)?\b([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})\b
See demo 2
Also, if the hyphen in \+1-
is optional, add a ?
: \+1-?
.
To make the regex safer, I'd replace \D*
(0 or more non-digit symbols) with some character class containing known separators, e.g [ /-]*
(matching /
, spaces and -
s).
Upvotes: 0
Reputation: 74345
try something like this to make things more readable:
Regex rxPhoneNumber = new Regex( @"
^ # anchor the start-of-match to start-of-text
\D* # - allow and ignore any leading non-digits
1? # - we'll allow (and ignore) a leading 1 (as in 1-800-123-4567
\D* # - allow and ignore any non-digits following that
(?<areaCode>[2-9]\d\d) # - required 3-digit area code
\D* # - allow and ignore any non-digits following the area code
(?<exchangeCode>[2-9]\d\d) # - required 3-digit exchange code (central office)
\D* # - allow and ignore any non-digits following the C.O.
(?<subscriberNumber>\d\d\d\d) # - required 4-digit subscriber number
\D* # - allow and ignore any non-digits following the subscriber number
$ # - followed the end-of-text.
" ,
RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture
);
string input = "voice: 1 (234) 567/1234 (leave a message)" ;
bool isValid = rxPhoneNumber.IsMatch(input) ;
string tidied = rxPhoneNumber.Replace( input , "+1-${areaCode}-${exchangeCode}-${subscriberNumber}" ) ;
which will give tidied
the desired value
+1-234-567-1234
Upvotes: 1