none
How to write a regular expression which has two letters separated by comma and specific combination will not allowed other than everything will allow. RRS feed

  • Question

  • I have a string 700XXX(5b,c). In this string XXX can be any letters and (5b,c) will not allowed to match but combination of e.g

    700XXX(5a-z,c) except 'b' is not allowed e.g 700XXX(5d,c),700XXX(5f,c) is allowed like that

    700XXX(5b,a-z) except 'c' is not allowed e.g 700XXX(5b,a), 700XXX(5b,k) is allowed like that

    Can anybody help me to write regular expression for this string?


    • Edited by mahadeb Friday, November 15, 2013 6:51 AM
    Friday, November 15, 2013 6:07 AM

Answers

  • Hello,

    Have a try codes below:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string input = Console.ReadLine();
    
    
                Regex reg = new Regex(@"^700[\w\W]*(\(5[a-z]\,[c]\)|\(5[b]\,[a-z]\))$");
    
                bool result = reg.IsMatch(input);
    
                Console.WriteLine(result);
    
                Console.ReadLine();
    
            }
    
        }
    

    One thing you need to do is to make sure which letter is allowed in the expression.

    So we can change codes to be below:

    static void Main(string[] args)
    
            {
    
                string input = Console.ReadLine();
    
    
                string firstallowedletter = "c";
    
                string secondallowedletter = "b";
    
                string expression = @"^700[\w\W]*(\(5[a-z]\,[" + firstallowedletter + "]\\)|\\(5[" + firstallowedletter + "]\\,[a-z]\\))$";
    
                Regex reg = new Regex(expression);
    
                bool result = reg.IsMatch(input);
    
                Console.WriteLine(result);
    
                Console.ReadLine();
    
            }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 18, 2013 7:13 AM
    Moderator
  • I think what you really want is to call Regex twice.  Once to get the parameter of the search, and then to validate that the pattern meets you inputs.  See code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filter= "700XXX(5a-z,c)";
                string pattern1 = @"^(?'p1'\d*)(?'p2'[^\(]*)\((?'p3'\d*)(?'p4'[^,]*),(?'p5'[^\)]*)";
                Regex expr1 = new Regex(pattern1);
                Match match1 = expr1.Match(filter);
                string p1 = match1.Groups["p1"].Value;
                string p2 = match1.Groups["p2"].Value;
                string p3 = match1.Groups["p3"].Value;
                string p4 = "[" + match1.Groups["p4"].Value + "]";
                string p5 = match1.Groups["p5"].Value;
                string pattern2 = string.Format(@"^(?'p1'{0})(?'p2'{1})\((?'p3'{2})(?'p4'{3}),(?'p5'{4})",
                    p1,p2,p3,p4,p5);
                string input = "700XXX(5d,c)";
                Regex expr2 = new Regex(pattern2);
                Boolean found = expr2.IsMatch(input);
            }
        }
    }


    jdweng

    Monday, November 18, 2013 9:16 AM

All replies

  • Hello,

    Have a try codes below:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string input = Console.ReadLine();
    
    
                Regex reg = new Regex(@"^700[\w\W]*(\(5[a-z]\,[c]\)|\(5[b]\,[a-z]\))$");
    
                bool result = reg.IsMatch(input);
    
                Console.WriteLine(result);
    
                Console.ReadLine();
    
            }
    
        }
    

    One thing you need to do is to make sure which letter is allowed in the expression.

    So we can change codes to be below:

    static void Main(string[] args)
    
            {
    
                string input = Console.ReadLine();
    
    
                string firstallowedletter = "c";
    
                string secondallowedletter = "b";
    
                string expression = @"^700[\w\W]*(\(5[a-z]\,[" + firstallowedletter + "]\\)|\\(5[" + firstallowedletter + "]\\,[a-z]\\))$";
    
                Regex reg = new Regex(expression);
    
                bool result = reg.IsMatch(input);
    
                Console.WriteLine(result);
    
                Console.ReadLine();
    
            }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 18, 2013 7:13 AM
    Moderator
  • I think what you really want is to call Regex twice.  Once to get the parameter of the search, and then to validate that the pattern meets you inputs.  See code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filter= "700XXX(5a-z,c)";
                string pattern1 = @"^(?'p1'\d*)(?'p2'[^\(]*)\((?'p3'\d*)(?'p4'[^,]*),(?'p5'[^\)]*)";
                Regex expr1 = new Regex(pattern1);
                Match match1 = expr1.Match(filter);
                string p1 = match1.Groups["p1"].Value;
                string p2 = match1.Groups["p2"].Value;
                string p3 = match1.Groups["p3"].Value;
                string p4 = "[" + match1.Groups["p4"].Value + "]";
                string p5 = match1.Groups["p5"].Value;
                string pattern2 = string.Format(@"^(?'p1'{0})(?'p2'{1})\((?'p3'{2})(?'p4'{3}),(?'p5'{4})",
                    p1,p2,p3,p4,p5);
                string input = "700XXX(5d,c)";
                Regex expr2 = new Regex(pattern2);
                Boolean found = expr2.IsMatch(input);
            }
        }
    }


    jdweng

    Monday, November 18, 2013 9:16 AM