Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have to write a regular expression to get repeated digits in a number.

Ex: 3445

I have to get 44 as answer..

Please help me to write the regular expression in c#.

I am not good at regex

Thank you
Posted

Try this :
(\d)\1+
 
Share this answer
 
You can achieve the same by using Linq query:
C#
int myNumber = 3445;

var qry = from n in myNumber.ToString()
		group n by n into grp
		where grp.Count()>1
		select new{Number=grp.Key, Count=grp.Count()};

foreach(var c in qry)
{
    Console.WriteLine("{0} - {1}", c.Number, c.Count);
}

Result:
4 - 2
 
Share this answer
 
Comments
BillWoodruff 4-Dec-14 8:30am    
Note: by sharing this I do NOT intend to imply you copied your answer ! I just thought you might enjoy seeing that a similar technique was used elsewhere.

Reminds me of this:

http://stackoverflow.com/a/829641/133321

cheers, Bill

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