public static void TestRegexMatch1(afh.Application.Log log) { using (log.Lock()){ StringRegex.RegLan reglan = new StringRegex.RegLan("こんにちは|さようなら"); log.WriteLine("Regular Expression: {0}", reglan); foreach (StringRegex.Capture c in reglan.Matches("こんにちは さようなら こんにちは こんにちは さようなら")) { log.WriteLine("Match! : {0}", c.Value); } reglan = new StringRegex.RegLan(@"(?<l>\d+)(?<o>(?:\+|\-))(?<r>\d+)"); log.WriteLine("Regular Expression: {0}", reglan); foreach (StringRegex.Capture c in reglan.Matches("3+2+4+5 1+2+3 2+3 4+7 123+321")) { int l = int.Parse(c.Groups["l"].Last); int r = int.Parse(c.Groups["r"].Last); bool o = c.Groups["o"].Last.Value == "+"; log.WriteLine("{0} を計算すると→: {1}", c, o?l + r:l - r); } log.WriteLine("== System.Text.RegularExpressions の場合 =="); Rgx::Regex rgx = new Rgx::Regex(@"(?<l>\d+)(?<o>(?:\+|\-))(?<r>\d+)"); log.WriteLine("Regular Expression: {0}", rgx.ToString()); foreach (Rgx::Match m in rgx.Matches("3+2+4+5 1+2+3 2+3 4+7 123+321")) { int l = int.Parse(m.Groups["l"].Value); int r = int.Parse(m.Groups["r"].Value); bool o = m.Groups["o"].Value == "+"; log.WriteLine("{0} を計算すると→: {1}", m.Value, o?l + r:l - r); } } }
public static int benchRegexMatchAfh2() { int s = 0; foreach (StringRegex.Capture c in reglan2.Matches(@" adsasda ""還元"" dadas ""Hello!"" dasda22 ""充君"" adaaee ""三軒茶屋"" adad")) { s += c.Groups[0].Last.Value.Length; } return(s); }
public static int benchRegexMatchAfh1() { int s = 0; foreach (StringRegex.Capture c in reglan.Matches("3+2+4+5 1+2+3 2+3 4+7 123+321")) { int l = int.Parse(c.Groups["l"].Last); int r = int.Parse(c.Groups["r"].Last); bool o = c.Groups["o"].Last.Value == "+"; s += o?l + r:l - r; } return(s); }