Reputation: 1702
I am using following lines of code
<asp:TemplateField HeaderText="Phone" SortExpression="Phone">
<ItemTemplate>
<asp:Label ID="lblPhone" runat="server">
<%# Regex.Replace(Eval("Phone").ToString(), @"(\d{3})(\d{3})(\d{4})", "($1)-$2-$3") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It is formatting numbers like 9419002345 but not numbers like 1408-464-1680 or 463237062... Please help me in writing accurate regular expression.
Upvotes: 0
Views: 26
Reputation: 4423
You can try this:
(\d{10})
will compare 10 numbers
OR
4 digits
-3 digits
-4 digits
.
(\d{10})|(\d{4}-\d{3}-\d{4})
Adjust number of digits as per your need.
Upvotes: 1
Reputation: 157048
That is because your regular expression just matches when there are ten digits. Your 463237062
has only 9.
You should make the algorithm allow matching on 9 digits too, for example like this:
@"(\d{1,3})(\d{3})(\d{4})"
Upvotes: 1