Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

New to Regular Expression : Question about \d ?

Last post 07-30-2010, 2:04 PM by buckley. 3 replies.
Sort Posts: Previous Next
  •  07-30-2010, 12:36 PM 70296

    New to Regular Expression : Question about \d ?

    Hello everyone,

    I want to write a Reg that matches a RGB color. For example : 255,250,100, and for each spot, a number can range from 1-256. That means a number with 1 or 2 or 3 digits would satisfy, so I came up with this solution:

    Regex reg_0 = new Regex( "\\d{[1-3]},\\d{[1-3]}" );

    However, when I tried to check with this string :

    string color = "12,12";

    Console.WriteLine( reg_0.IsMatch( color ) );

    Console.WriteLine( reg_0.IsMatch( color ) );

    string color = "12,12";

    Console.WriteLine( reg_0.IsMatch( color ) );

    Console.WriteLine( reg_0.IsMatch( color ) );

     It failed :( ! From my understanding, \d matches any digit, {} is a quantifier, [] is the class character. Anyone could give me a hint where did I miss?

     

    Thanks,

  •  07-30-2010, 1:11 PM 70298 in reply to 70296

    Re: New to Regular Expression : Question about \d ?

    First off using a regex to test a range is not the best application of a regular expression.  It can be done but can get tricky.

    As to your problem your mistake is that you are trying to put a pattern inside your qualifier. Qualifiers are part of the pattern they themselves don't contain any regex patterns.  If you want to select 1 to 3 digits the pattern would be \d{1,3}

    While that would match any of the values in your range it would also match any 3 digit value outside of your range. As I said before range matching is a poor application of regex because regular expressions have no concept of numbers. So you  pattern has to match every string representation of the numbers in your range. That patterns can get  pretty complex when your range endpoints don't end with a zero.

    Below is a pattern for  0 to 255 (not 1 to 256)

    Raw Match Pattern:
    \b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b

    Match Pattern Explanation:
    The regular expression:

    (?-imsx:\b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b)

    matches as follows:
      
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching \n) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
      (?:                      group, but do not capture:
    ----------------------------------------------------------------------
        (?:                      group, but do not capture:
    ----------------------------------------------------------------------
          2                        '2'
    ----------------------------------------------------------------------
          (?:                      group, but do not capture:
    ----------------------------------------------------------------------
            (?:                      group, but do not capture:
    ----------------------------------------------------------------------
              [0-4]                    any character of: '0' to '4'
    ----------------------------------------------------------------------
              \d                       digits (0-9)
    ----------------------------------------------------------------------
            )                        end of grouping
    ----------------------------------------------------------------------
           |                        OR
    ----------------------------------------------------------------------
            (?:                      group, but do not capture:
    ----------------------------------------------------------------------
              5                        '5'
    ----------------------------------------------------------------------
              [0-5]                    any character of: '0' to '5'
    ----------------------------------------------------------------------
            )                        end of grouping
    ----------------------------------------------------------------------
          )                        end of grouping
    ----------------------------------------------------------------------
        )                        end of grouping
    ----------------------------------------------------------------------
       |                        OR
    ----------------------------------------------------------------------
        (?:                      group, but do not capture:
    ----------------------------------------------------------------------
          1?                       '1' (optional (matching the most
                                   amount possible))
    ----------------------------------------------------------------------
          \d                       digits (0-9)
    ----------------------------------------------------------------------
          \d?                      digits (0-9) (optional (matching the
                                   most amount possible))
    ----------------------------------------------------------------------
        )                        end of grouping
    ----------------------------------------------------------------------
      )                        end of grouping
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------

    C#.NET Code Example:

    using System;
    using System.Text.RegularExpressions;
    namespace myapp
    {
      class Class1
        {
          static void Main(string[] args)
            {
              String sourcestring = "source string to match with pattern";
              Regex re = new Regex(@"\b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b");
              MatchCollection mc = re.Matches(sourcestring);
              int mIdx=0;
              foreach (Match m in mc)
               {
                for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
                  {
                    Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
                  }
                mIdx++;
              }
            }
        }
    }


    $matches Array:
    (
        [0] => Array
            (
                [0] => 2
                [1] => 1
                [2] => 10
                [3] => 35
                [4] => 152
                [5] => 193
                [6] => 205
                [7] => 251
            )

    )


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-30-2010, 1:53 PM 70299 in reply to 70298

    Re: New to Regular Expression : Question about \d ?

    Hi Mash,

     Thanks a lot for your "extremely detailed" explanation ^_^ ! But you were trying to screw me, weren't you ^_^ ? Just kidding. I'll try to play around with it.

    Thanks,

     

  •  07-30-2010, 2:04 PM 70300 in reply to 70299

    Re: New to Regular Expression : Question about \d ?

    You were close in your original regex :

    \d{[1-3]},\d{[1-3]}

    It should be :


    \d{1,3},\d{1,3},\d{1,3}

    notice that I removed the [] symbols since they are used for representing a character class

    note that I replaced the - symbol because its used to represent a range in a character class and that's not what you need here. The , instead of the - says that the preceding character can occur 1 tot 3 times

    I also added a third digit to the regex because you need to match RGB right? And not RG?

    HTH, Tom Pester

View as RSS news feed in XML