.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*");
}
}
精彩评论