Regular Expression for PICS

Hello,

I have been trying (unsuccessfullly) for a short time to right a regex to parse a RAT file as used to load content rating information.

Forgetting the file header, the data is grouped like this:

(category (transmit-as "SS~~000") (name "Age Range")
   (min 1) (max 2) (label-only true) (integer true)
   (label
     (name "All Ages")
     (value 1))
   (label
     (name "Older Children")
     (value 2)))

 (category
  (transmit-as "og")
  (name "Other Topics - Material that might be perceived as setting a bad example for young children.")
   (label
   (name "")
   (description "Do not allow access to sites that contain images, portrayals or descriptions that might be perceived as setting a bad example for young children.")
   (value 0) )
   (label
   (name "")
   (description "Allow access to sites that contain images, portrayals or descriptions that might be perceived as setting a bad example for young children.")
   (value 1) ))

(Yes the catagories can end with either a ) )) or a ))) just to make it fun)

The above two categroies are from different RAT files, but I would like a single expression (if at all possible) to look after either format.

I have a regex that matches Categories only, but I am stuck on one that will match the labels.  If this could include Capture-By-Name (?<name>) it would be good.
I would like to match:

<label> <name> <description> <value>
-- NEXT MATCH ---
<label> <name> <description> <value>
-- NEXT MATCH ---

I hope this makes sense?  I'm being lazy here - I could spend another few hours on this, but it is Zzzz time - so I'm offerring 500 to the first person who supplies the required regex for me :-)

Many thanks
Si-cloneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NipNFriar_TuckCommented:
You might try something like:

     public struct ARec {
        public string CATEGORY;
        public string LABEL;
        public string VALUE;
        public string DESCRIPTION;
    }


           Regex regex = new Regex(
                @"category|label[\s\r\n]+|name\s\""(?<NAME>[0-9A-Za-z\s\-\.]+)"
                + @"|description\s\""(?<DESCRIPTION>[0-9A-Za-z\s\-\.\,]+)|value\s"
                + @"(?<VALUE>[0-9A-Za-z\s\-\.]+)",
                RegexOptions.IgnoreCase
                | RegexOptions.Multiline
                | RegexOptions.ExplicitCapture
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
                );

            MatchCollection matches = regex.Matches(pic);
            bool isCategory = false;
            bool isComplete = false;
            ARec[] recs = new ARec[10];
            int count = 0;
            foreach ( Match match in matches ) {
                if ( isComplete ) {
                    count++;
                    isComplete = false;
                }

                if ( match.Value.IndexOf("category") >= 0 ) {
                    isCategory = true;
                }

                if ( match.Groups["NAME"].Success ) {
                    if ( isCategory ) {
                        recs[count].CATEGORY = match.Groups["NAME"].Value;
                        isCategory = false;
                    } else {
                        recs[count].LABEL = match.Groups["NAME"].Value;
                    }
                } else if ( match.Groups["DESCRIPTION"].Success ) {
                    recs[count].DESCRIPTION = match.Groups["DESCRIPTION"].Value;
                } else if  ( match.Groups["VALUE"].Success ) {
                    recs[count].VALUE = match.Groups["VALUE"].Value;
                    isComplete = true;
                }
            }

This will give you an array of structures with the fields of category, label, value and description... of course a class may server your purpose better...


HTH
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Si-cloneAuthor Commented:
Thanks for your post.  This was exactly what I was looking for, but showed me to open my eyes to other avenues.

Cheers,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.