读书人

利用正则式找出相同数字

发布时间: 2012-08-07 14:54:48 作者: rapoo

利用正则式找到相同数字
有几行数字,每行的数字有3位,在3个数字中有可能某个数字会出现两次,能不能用正则表达式找到带有相同数字的3个数字。
例如:
123
548
668
842
848
112
054
008
655

在上面的几行数字中要找到668,848,112,008,655这几个数字。
求解。

[解决办法]

C# code
            StreamReader reader = new StreamReader("c:\\1.txt");            string source = reader.ReadToEnd();            Regex reg = new Regex(@".*(\d).*\1+.*");                        MatchCollection mc = reg.Matches(source);            foreach (Match m in mc)            {                MessageBox.Show(m.Value);            }
[解决办法]
if(Regex.IsMatch(str,@"(\d)\d*\1"))
{
Console.WriteLine(str);
}

读书人网 >VB Dotnet

热点推荐