Regex to retrieve phone numbers
-
Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following:
"John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345"
What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example:customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234";
I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help? -
Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following:
"John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345"
What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example:customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234";
I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help?You may find this solution just made for you Helpful.
string Text = "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, ";
Regex HomePhoneRegex = new Regex(@"\(H\)[ ]*\d{13}");
Regex WorkPhoneRegex = new Regex(@"\(W\)[ ]*\d{13}");
Regex EmailRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");String HomePhone = String.Empty; String WorkPhone = String.Empty; String Email = String.Empty; if (HomePhoneRegex.Matches(Text).Count == 1) { HomePhone = HomePhoneRegex.Matches(Text)\[0\].Value.Replace("(H)", String.Empty).Trim(); } if (WorkPhoneRegex.Matches(Text).Count == 1) { WorkPhone = WorkPhoneRegex.Matches(Text)\[0\].Value.Replace("(W)", String.Empty).Trim(); } if (EmailRegex.Matches(Text).Count == 1) { Email = EmailRegex.Matches(Text)\[0\].ToString(); }
Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".
-
You may find this solution just made for you Helpful.
string Text = "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, ";
Regex HomePhoneRegex = new Regex(@"\(H\)[ ]*\d{13}");
Regex WorkPhoneRegex = new Regex(@"\(W\)[ ]*\d{13}");
Regex EmailRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");String HomePhone = String.Empty; String WorkPhone = String.Empty; String Email = String.Empty; if (HomePhoneRegex.Matches(Text).Count == 1) { HomePhone = HomePhoneRegex.Matches(Text)\[0\].Value.Replace("(H)", String.Empty).Trim(); } if (WorkPhoneRegex.Matches(Text).Count == 1) { WorkPhone = WorkPhoneRegex.Matches(Text)\[0\].Value.Replace("(W)", String.Empty).Trim(); } if (EmailRegex.Matches(Text).Count == 1) { Email = EmailRegex.Matches(Text)\[0\].ToString(); }
Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".
Thanks that did help :)
-
Thanks that did help :)
You're welcome. :) Glad it helped you.
Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".
-
You're welcome. :) Glad it helped you.
Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".
-
I am using this tool every time I need fun with Regex. :)
Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".
-
Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following:
"John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345"
What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example:customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234";
I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help?We also have a RegEx forum.