简体   繁体   中英

Extract many phone numbers using RegEx

I want to extract phone numbers from following regex. It extracts all phone number formats when I input numbers one by one but not when I input 2 or more numbers in one string.

@"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";

Of course it won't extract multiple numbers. The expression starts with ^ and ends with $ , so if the string contains anything other than a single phone number, the regular expression will fail to match.

One solution is to remove those start and end characters, and then loop to extract the numbers one at a time.

Regex rePhone = new Regex(@"\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d");
Match m = rePhone.Match(inputString);
while (m.Success)
{
    string phone = m.Value;
    m = m.NextMatch();
}

试试这个模式:

((\+\s|\d{1}\s?\+||\()(\d\)?\s?[-\.\s\(]??){8,}\d{1}|\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})

for extract all phone Numbers you can try this Regex :

((\+|\+\s|\d{1}\s?|\()(\d\)?\s?[-\.\s\(]??){8,}\d{1}|\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})

its math often phone numbers. you can check yor phone numbers with this Regex in Regex101.com

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM