现在的位置: 首页 > 综合 > 正文

ASP.NET正则表达式查找字符串中重复的字符

2013年10月14日 ⁄ 综合 ⁄ 共 962字 ⁄ 字号 评论关闭
using System;
using System.Text.RegularExpressions;

public class Test
{

    
public static void Main ()
    
{

        
// Define a regular expression for repeated words.
        Regex rx = new Regex(@"(?<word>w+)s+(k<word>)",
          RegexOptions.Compiled 
| RegexOptions.IgnoreCase);

        
// Define a test string.        
        string text = "The the quick brown fox  fox jumped over the lazy dog dog.";
        
        
// Find matches.
        MatchCollection matches = rx.Matches(text);

        
// Report the number of matches found.
        Console.WriteLine("{0} matches found.", matches.Count);

        
// Report on each match.
        foreach (Match match in matches)
        
{
            
string word = match.Groups["word"].Value;
            
int index = match.Index;
            Console.WriteLine(
"{0} repeated at position {1}", word, index);   
        }

        
    }

    
}

 http://www.microsoft.com/china/msdn/library/webservices/asp.net/regexnet.mspx?mfr=true

 

抱歉!评论已关闭.