language: C# (mono-2.8)
date: 730 days 8 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Text.RegularExpressions;
 
/*
http://kobikobi.wordpress.com/2011/05/19/net-regular-expressions-finding-decimal-numbers-that-are-divisible-by-three/
*/
 
public class Test
{
        public static void Main()
        {
                string pattern = @"
\b
(?>             # No regrets - don't backtrack on if/else decisions.
    [0369]      # = 0 (mod 3)
    |
    [147]       # = 1 (mod 3)
    (?:         # if possible pop 2, else push 1
        (?<-Sum>){2}|(?<Sum>)
    )
    |
    [258]       # = 2 (mod 3)
    (?:         # if possible pop 1, else push 2
        (?<-Sum>)|(?<Sum>){2}
    )
)+
\b
(?(Sum)(?!)) # Assert nothing's left in the stack
";
                string text = Console.ReadLine();
                MatchCollection matches = Regex.Matches(text, pattern,
                        RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
                foreach (Match match in matches)
                {
                   Console.WriteLine(match.Value);
                }
        }
}
Code sample for http://kobikobi.wordpress.com/2011/05/19/net-regular-expressions-finding-decimal-numbers-that-are-divisible-by-three/