Example #1
0
        public void Test_IsMatch_36()
        {
            const string EXPECTED = @"^(\(?\d{3}\)?[\s-])?\d{3}-\d{4}$";

            var pattern = new RegExpPattern()
                          .AtStartOfStringOrLine()
                          .ZeroOrOneOf(
                false, true,
                p => p.ZeroOrOneOf(
                    false, false,
                    p2 => p2.Character('('))
                .Time(3, p2 => p2.AnyOneDigit())
                .ZeroOrOneOf(
                    false, false,
                    p2 => p2.Character(')'))
                .AnyCharacter(
                    RegExpEscapeCharacter.Build(EscapeCharacter.EscapeCharacterType.WhiteSpace),
                    RegExpCharacter.Build('-')))
                          .Time(3, p => p.AnyOneDigit())
                          .Text("-")
                          .Time(4, p => p.AnyOneDigit())
                          .AtEndOfStringOrLine();

            pattern.ToString().Should().Be(EXPECTED);
        }
Example #2
0
        public static RegExpPattern Character(
            this RegExpPattern pattern,
            char character,
            bool asUnicode = false
            )
        {
            pattern.AddRule(new RegExpValue($"{RegExpCharacter.Build(character, asUnicode)}"));

            return(pattern);
        }
Example #3
0
        public void Test_Negative_Any_Of_One_Character_4_Pattern()
        {
            const string EXPECTED = "[^\\t\\u002A]";

            var pattern = new RegExpPattern()
                          .AnyCharacterOtherThan(
                RegExpEscapeCharacter.Build(EscapeCharacter.EscapeCharacterType.Tab),
                RegExpCharacter.Build('*', true));

            pattern.ToString().Should().Be(EXPECTED);
        }
Example #4
0
        public void Test_Any_Of_One_Character_3_Pattern()
        {
            const string EXPECTED = "[\\t*]";

            var pattern = new RegExpPattern()
                          .AnyCharacter(
                RegExpEscapeCharacter.Build(EscapeCharacter.EscapeCharacterType.Tab),
                RegExpCharacter.Build('*'));

            pattern.ToString().Should().Be(EXPECTED);
        }
Example #5
0
        public static RegExpPattern AnyCharacterOtherThan(
            this RegExpPattern pattern,
            string characters)
        {
            var concatChars = characters.Aggregate(
                string.Empty,
                (seed, character) =>
                seed + RegExpCharacter.Build(character)
                );

            pattern.AddRule(new RegExpValue($"[^{concatChars}]"));

            return(pattern);
        }
Example #6
0
        public void Test_IsMatch_25()
        {
            const string EXPECTED = @"\bth[^o]\w+\b";

            var pattern = new RegExpPattern()
                          .WithWordBoundary(
                p => p
                .Text("th")
                .AnyCharacterOtherThan(RegExpCharacter.Build('o'))
                .OneOrMoreOf(false, false,
                             p3 => p3.AnyWordCharacter()));

            pattern.ToString().Should().Be(EXPECTED);
        }
Example #7
0
        public void Test_IsMatch_29()
        {
            const string EXPECTED_REGEX_PATTERN = @"\G(.+)[\t\u007C](.+)\r?\n";

            var pattern = new RegExpPattern()
                          .ByOnlyMatchingWherePreviousMatchEnded()
                          .GroupOf(
                true,
                p => p.OneOrMoreOf(
                    false, false,
                    p2 => p2.AnyCharacter())
                )
                          .AnyCharacter(
                RegExpEscapeCharacter.Build(EscapeCharacter.EscapeCharacterType.Tab),
                RegExpCharacter.Build('|', true))
                          .GroupOf(
                true,
                p => p
                .OneOrMoreOf(
                    false, false,
                    p2 => p2.AnyCharacter()))
                          .ZeroOrOneOf(
                false, false,
                p => p.Character(EscapeCharacter.EscapeCharacterType.CarriageReturn))
                          .Character(EscapeCharacter.EscapeCharacterType.NewLine);

            pattern.ToString().Should().Be(EXPECTED_REGEX_PATTERN);

            const string INPUT = "Mumbai, India|13,922,125\t\n" +
                                 "Shanghai, China\t13,831,900\n" +
                                 "Karachi, Pakistan|12,991,000\n" +
                                 "Delhi, India\t12,259,230\n" +
                                 "Istanbul, Turkey|11,372,613\n";

            Console.WriteLine("Population of the World's Largest Cities, 2009");
            Console.WriteLine();
            Console.WriteLine("{0,-20} {1,10}", "City", "Population");
            Console.WriteLine();

            foreach (Match match in pattern.Matches(INPUT))
            {
                Console.WriteLine("{0,-20} {1,10}",
                                  match.Groups[1].Value,
                                  match.Groups[2].Value);
            }
        }
Example #8
0
        public static RegExpPattern AnyCharacter(
            this RegExpPattern pattern,
            string characters)
        {
            var concatChars = characters.Aggregate(
                string.Empty,
                (seed, character) =>
            {
                var rec        = (RegExpCharacter)RegExpCharacter.Build(character);
                rec.EscapeChar = false;

                return(seed + rec);
            }
                );

            pattern.AddRule(new RegExpValue($"[{concatChars}]"));

            return(pattern);
        }
Example #9
0
        public static RegExpPattern Text(this RegExpPattern pattern, string value, bool escapeCharacter = true)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(pattern);
            }

            var concatChars = value
                              .Aggregate(
                string.Empty,
                (seed, character) =>
            {
                var reC        = (RegExpCharacter)RegExpCharacter.Build(character);
                reC.EscapeChar = escapeCharacter;

                return(seed + reC);
            }
                );

            pattern.AddRule(new RegExpValue(concatChars));

            return(pattern);
        }