[Solved] regex to exact match 11 digit phone number from string and remove hyphen(-) from match in c#


You can use below Regex to remove all hyphens and all other non numeric characters from phone number

string pattern = @"\b([\-]?\d[\-]?){11}\b";

Regex rgx = new Regex(pattern);

var sentence = "This is phone number 0341-2239548 and 021-34223311";

var matches = rgx.Matches(sentence);

foreach (Match match in matches)
{
    string replacedValue = Regex.Replace(match.Value, @"[^0-9]", "");
    sentence = sentence.Replace(match.Value, replacedValue);
}

See the demo

6

solved regex to exact match 11 digit phone number from string and remove hyphen(-) from match in c#