Probably an easy regex question.
How do I remove all non-digts except leading + from a phone number?
i.e.
012-3456 => 0123456
+1 (234) 56789 => +123456789
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
will remove all non-numbers and leave a leading
+alone. Note that leading whitespace will cause the “leave+alone” bit to fail. In .NET languages, this can be worked into the regex, in others you should strip whitespace first before passing the string to this regex.Explanation:
(?<!^)\+: Match a+unless it’s at the start of the string. (In .NET, use(?<!^\s*)\+to allow for leading whitespace).|or[^\d+]+: match any run of characters that are neither numbers nor+.Before (using
(?<!^\s*)\+|[^\d+]+):After: