I have strings with un-formatted phone numbers, is there any way to find them and format the number and save?
Sample strings I have are
Name: Tippu
Phone: 408.273.3939
Email: tippu@tippu.com
another sample
Contact Tippu at (408) 363 2323
In these examples I need to find the phone number and change it to NNN-NNN-NNNN format. Data is in SQL Server and I am using c#.
Thanks
You can use the following pseudocode.
string.Replace(/\(?(\d{3})[)(-.\s]*(\d{3})[)-.\s]*(\d{4})/, "$1-$2-$3")
It will replace both the provided examples in NNN-NNN-NNNN
format.
Let me explain you the regex a bit.
\\(?
in the beginning looks if the string is starting with a bracket
(\\d{3})
looks for three numeric digits, and make a capturing group. [)(-.\\s]*
looks for )
(
-
.
and a space
. *
makes sure to take care of the case where bracket is followed by a space.
\\d{4}
looks for 4 digits.
Here $1
$2
and $3
are the capture groups. You can use them according to your language.
I believe, you can probably use Regex.Replace()
function in C#
Probably not the best... But this is something like what I would do if it were me. It might at least help. Firstly, I assume that the last numbers in your strings represent the phone number:
x = <your string>.replace(/[^0-9]/g,'');
x = x.substring(x.length-10);
x = x.substring(0,3) +'-'+ x.substring(3,6) + '-' + x.substring(6);
Assuming Your Phone number is in this format - "408.273.3939" then this would be helpful to you
var stringText = "408.273.3939";
var phoneArr = stringText.Split('.');
String.Format("%s-%s-%s", phoneArr[0], phoneArr[1], phoneArr[2]);
Moreover your question is also not clear to me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.