Csharp/CSharp Tutorial/Regular Expression/Regex Money — различия между версиями

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

Текущая версия на 12:17, 26 мая 2010

Extracting groups from money matches

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{1,3},)*\d+)\.(\d{2})");
        Match m = r.Match("$1,035.66,100.99");
        Console.WriteLine("Dollar part: {0}, cents: {1}",
            m.Groups[1].Captures[0].Value, m.Groups[3].Captures[0].Value);
   }
}
Dollar part: 1,035, cents: 66

Matching money: \$(((\d{1,3},)+\d{3})|\d+)\.\d{2}

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 moneyR = new Regex(@"\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}");
        string[] money = new string[] { "$0.99", 
                                        "$1099999.00", 
                                        "$10.25", 
                                        "$90,999.99", 
                                        "$1,990,999.99", 
                                        "$1,999999.99" };
        foreach (string m in money){
            Console.WriteLine(m);
            Console.WriteLine(moneyR.IsMatch(m));
        }
   }
}
$0.99
True
$1099999.00
True
$10.25
True
$90,999.99
True
$1,990,999.99
True
$1,999999.99
False

Matching money: \$(\d{1,3},)*\d+\.\d{2}

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 moneyR = new Regex(@"\$(\d{1,3},)*\d+\.\d{2}");
        string[] money = new string[] { "$0.99", 
                                        "$1099999.00", 
                                        "$10.25", 
                                        "$90,999.99", 
                                        "$1,990,999.99", 
                                        "$1,999999.99" };
        foreach (string m in money){
            Console.WriteLine(m);
            Console.WriteLine(moneyR.IsMatch(m));
        }
   }
}
$0.99
True
$1099999.00
True
$10.25
True
$90,999.99
True
$1,990,999.99
True
$1,999999.99
True

Matching money: \$\d+\.\d{2}

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 moneyR = new Regex(@"\$\d+\.\d{2}");
        string[] money = new string[] { "$0.99", 
                                        "$1099999.00", 
                                        "$10.25", 
                                        "$90,999.99", 
                                        "$1,990,999.99", 
                                        "$1,999999.99" };
        foreach (string m in money){
            Console.WriteLine(m);
            Console.WriteLine(moneyR.IsMatch(m));
        }
   }
}
$0.99
True
$1099999.00
True
$10.25
True
$90,999.99
False
$1,990,999.99
False
$1,999999.99
False