Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need a regular expression for below format

(111)222-3333 (10digits only)
11122212345 (10 or 11 digits)
111-222-1234 (10digits only)
Posted
Comments
Santosh K. Tripathi 5-Mar-15 6:16am    
what you have tried?
vr reddy 5-Mar-15 6:17am    
my requirement is 3 points
1.(111)222-3333 (10digits only)
2.11122212345 (10 or 11 digits)
3.111-222-1234 (10digits only)

^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})\d?$ (i added this line)
(but 1 and 3 points also accepted 11 characters
vr reddy 5-Mar-15 6:23am    
please help
TheRealSteveJudge 5-Mar-15 9:20am    
Please have a look at solution 2.

These regular expressions will catch the suggested inputs

(111)222-3333 (10digits only):
C#
\([0-9]{3}\)[0-9]{3}-[0-9]{4}


11122212345 (10 or 11 digits):
C#
[0-9]{10,11}


111-222-1234 (10digits only):
C#
[0-9]{3}-[0-9]{3}-[0-9]{4}


This will catch any of the given examples
C#
\([0-9]{3}\)[0-9]{3}-[0-9]{4}|[0-9]{10,11}|[0-9]{3}-[0-9]{3}-[0-9]{4}
 
Share this answer
 
v2
Comments
vr reddy 6-Mar-15 2:50am    
above three formats are only one text box how to separtate that 3 condtions
TheRealSteveJudge 6-Mar-15 4:55am    
Please see updated solution.
King Fisher 6-Mar-15 7:13am    
Very Nice my 5+
TheRealSteveJudge 6-Mar-15 7:21am    
Thank you!
King Fisher 6-Mar-15 7:26am    
Actually you done very well.I would link to learn Regex. Could you Refer me any links?
Try this:
using System;
using System.Text.RegularExpressions;

public class Program
{
	public static void Main()
	{
		Regex regex = new Regex(@"^\d{3}-\d{3}-\d{4}$|^\(\d{3}\)\d{3}-\d{4}$|^\d{10,11}$");
		Match match = regex.Match("11122212345");
		if (match.Success)
		{
			Console.WriteLine(match.Value);
		}
	}
}
 
Share this answer
 
v2
Comments
King Fisher 6-Mar-15 7:27am    
my 5, ;)
use this:

C#
\b\d{3}[-.]?\d{3}[-.]?\d{4}\b
 
Share this answer
 
Comments
TheRealSteveJudge 6-Mar-15 7:21am    
Also nice. 5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900