【问题标题】:.NET Regular Expression for N number of Consecutive CharactersN 个连续字符的 .NET 正则表达式
【发布时间】:2010-04-01 22:31:40
【问题描述】:

我需要一个匹配字符串中三个连续字符(任何字母数字字符)的正则表达式。

在哪里 2a82a9e4eee646448db00e3fccabd8c7 "eee" 将是一个匹配项。

在哪里 2a82a9e4efe64644448db00e3fccabd8c7 “444”将是一个匹配项。

等等

【问题讨论】:

  • @Chris:C# 没有正则表达式。
  • 为了将来参考,约翰的评论必须在发布日期的上下文中阅读。
  • @robert: @chris: 这些是 .NET 正则表达式,而不是 C# 正则表达式。
  • @John:Sheesh,约翰。你知道我们的意思。这是你说我们不够精确的方式吗?

标签: c# .net regex


【解决方案1】:

使用反向引用。

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

【讨论】:

    【解决方案2】:

    试试这个:

    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*");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2014-05-20
      • 2012-02-11
      • 1970-01-01
      相关资源
      最近更新 更多