Csharp/CSharp Tutorial/Regular Expression/Regex SIN

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

Social-security-number matching: [0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]

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("[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]");
        
        string s = "011-01-0111";
        string s2 = "abc-01-0111";
        
        Console.WriteLine(r1.ToString());
        Console.WriteLine(r1.IsMatch(s)); 
        Console.WriteLine(s2);
        Console.WriteLine(r1.IsMatch(s2));
   }
}
[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]
True
abc-01-0111
False

Social-security-number matching: [0-9]{3}-[0-9]{2}-[0-9]{4}

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("[0-9]{3}-[0-9]{2}-[0-9]{4}");
        
        string s = "011-01-0111";
        string s2 = "abc-01-0111";
        
        Console.WriteLine(r1.ToString());
        Console.WriteLine(r1.IsMatch(s)); 
        Console.WriteLine(s2);
        Console.WriteLine(r1.IsMatch(s2));
   }
}
[0-9]{3}-[0-9]{2}-[0-9]{4}
True
abc-01-0111
False