updated regexp solution for my digest auth

2007-12-10 @ 19:16#

i got a great suggestion from a fellow user of RegExBuddy this afternoon that solves my HTTP Ath Digest puzzle in a much nicer way. instead of solving for the proper Split, this solution solves for the correct set of Matches. this turns out to be much easier (i guess).

here's how my code loop looks now:

Regex rx = new Regex(@"([^=,""'\s]+\s*=\s*""[^""]*""|[^=,""'\s]+\s*=\s*'[^']*'|[^=,""'\s]+\s*=\s*[^=,""'\s]*)", RegexOptions.IgnoreCase);
Match mr = rx.Match(authHeader);
while (mr.Success)
{
    string[] pair = mr.Value.Split(new char[] { '=' }, 2);
    key = pair[0].Trim(new char[] { ' ', '\"' });
    string value = pair[1].Trim(new char[] { ' ', '\"' });
    dlist.Add(key, value);

    mr = mr.NextMatch();
} 

the regexp is a bit gnarly. primarily because it has several OR conditions to pick up various possible scenarios. here's the commented version of the expression:

@"
(                  # Match the regular expression below and capture its match into backreference number 1
                      # Match either the regular expression below (attempting the next alternative only if this one fails)
      [^=,""'\s]          # Match a single character NOT present in the list below
                            # One of the characters “=,""'”
                            # A whitespace character (spaces, tabs, line breaks, etc.)
         +                  # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      =                  # Match the character “=” literally
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      ""                  # Match the character “""” literally
      [^""]               # Match any character that is not a “""”
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      ""                  # Match the character “""” literally
   |                  # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      [^=,""'\s]          # Match a single character NOT present in the list below
                            # One of the characters “=,""'”
                            # A whitespace character (spaces, tabs, line breaks, etc.)
         +                  # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      =                  # Match the character “=” literally
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      '                  # Match the character “'” literally
      [^']               # Match any character that is not a “'”
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      '                  # Match the character “'” literally
   |                  # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      [^=,""'\s]          # Match a single character NOT present in the list below
                            # One of the characters “=,""'”
                            # A whitespace character (spaces, tabs, line breaks, etc.)
         +                  # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      =                  # Match the character “=” literally
      \s                 # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      [^=,""'\s]          # Match a single character NOT present in the list below
                            # One of the characters “=,""'”
                            # A whitespace character (spaces, tabs, line breaks, etc.)
         *                  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
"

much clearer, right [grin]?

code