Csharp/C Sharp by API/System.Text.RegularExpressions/Regex — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

new Regex(String pattern)

 

using System;
using System.Text.RegularExpressions;
class MainClass
{
    public static void Main()
    {
        string s = "A, B,cCCC, D";
        Regex regex = new Regex(@" |, ");
        foreach (string sub in regex.Split(s))
        {
            Console.WriteLine("Word: {0}", sub);
        }
    }
}


Regex.CompileToAssembly

  
using System;
using System.Reflection;
using System.Text.RegularExpressions;
class MainClass {
    public static void Main() {
        RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
        regexInfo[0] = new RegexCompilationInfo(@"^\d{4}$", RegexOptions.rupiled, "PinRegex", "", true);
        regexInfo[1] = new RegexCompilationInfo(@"^\d{4}-?\d{4}-?\d{4}-?\d{4}$", RegexOptions.rupiled, "CreditCardRegex", "", true);
        AssemblyName assembly = new AssemblyName();
        assembly.Name = "MyRegEx";
        Regex.rupileToAssembly(regexInfo, assembly);
    }
}


Regex.IsMatch

 
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
   public static void Main(){
        Regex r = new Regex("^\\d+");
        
        
        string[] ss = new string[] { "123abc", "abc123", "123", "abc", "a123bc" };
        foreach (string s in ss)
           Console.WriteLine(r.IsMatch(s));
   }
}


Regex.Match

 
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
   public static void Main(){
        Regex r1 = new Regex(@"((abc)*)x(\1)");
        string s1 = "abcabcabcxabc";
        Console.WriteLine(r1.IsMatch(s1) + "..." + r1.Match(s1).Value);
   }
}


Regex.Match(String value,String pattern).Success

  
using System;
using System.Text.RegularExpressions;
class RegexSubstitution
{
   public static void Main()
   {
      string text = "Name";
      if ( !Regex.Match(text,"^[A-Z][a-zA-Z]*$" ).Success )
      {
         Console.WriteLine( "Invalid last name");
      }
   }
}


Regex.Replace

 
using System;
using System.Text;
using System.Text.RegularExpressions;
public class MainClass
{
    static void Main( string[] args ) {
        // Create regex to search for IP address pattern.
        string pattern = @"(?<part1>[01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"(?<part2>[01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"(?<part3>[01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"(?<part4>[01]?\d\d?|2[0-4]\d|25[0-5])";
        Regex regex = new Regex( pattern );
        Match match = regex.Match( "192.168.123.1" );
        string replace = @"${part4}.${part3}.${part2}.${part1}" +
                         @" (the reverse of $&)";
        Console.WriteLine( regex.Replace("192.168.123.1", replace) );
    }
}


Regex.Split

  
using System;
using System.Text.RegularExpressions;

public class MainClass {
    public static void Main() {
        string x = "Once Upon A Time In America";
        Regex m = new Regex("( )");
        foreach (string ss in m.Split(x)) {
            Console.WriteLine(ss);
        }
    }
}