开发者

.NET Regular Expression for N number of Consecutive Characters

I need a regular expression that matches three consecutive characters (any alphanumeric character) in a string.

开发者_C百科

Where 2a82a9e4eee646448db00e3fccabd8c7 "eee" would be a match.

Where 2a82a9e4efe64644448db00e3fccabd8c7 "444" would be a match.

etc.


Use backreferences.

([a-zA-Z0-9])\1\1


Try this:

using System;
using System.Text.RegularExpressions;

class MainClass {

    private static void DisplayMatches(string text,
                                       string regularExpressionString) 
    {
        Console.WriteLine("using the following regular expression: " 
                        +regularExpressionString);
        MatchCollection myMatchCollection = 
            Regex.Matches(text, regularExpressionString);

        foreach (Match myMatch in myMatchCollection) {
          Console.WriteLine(myMatch);
        }
    }

    public static void Main() 
    {
        string text ="Missisipli Kerrisdale she";

        Console.WriteLine("Matching words that that contain " 
                          + "two consecutive identical characters");
        DisplayMatches(text, @"\S*(.)\1\S*");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜