老外编的程序(三)--正则表达式的Demo

来源:岁月联盟 编辑:zhu 时间:2003-07-12
using System.Text.RegularExpressions;

class Test {
    static string CapText(Match m) {
    // get the matched string
    string x = m.ToString();
    // if the first char is lower case
    if (char.IsLower(x[0])) {
        // capitalize it
        return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
    }
    return x;
    }
    
    static void Main() {
    string text = "four score and seven years ago";
    System.Console.WriteLine("text=[" + text + "]");
    string result = Regex.Replace(text, @"/w+",
                      new MatchEvaluator(Test.CapText));
    System.Console.WriteLine("result=[" + result + "]");
    }
}