Parsing string using regular expressions

Posted on 2007-10-12
Last Modified: 2010-04-15
Hi all,

I guess it's an easy question. Bun I'm new to regular expressions, so I need a little help :)

I have to parse a string. Format is like this: <mode>{<value>}. Mode is encoded in single character from some set, let's say A, B, and C. Value is a number.

A{1} - valid string
B{100} - valid
A{0} - invalid (value must be more than 0)  
A{-10} - invalid (value must be more than 0)  
A{0.5} - invalid (value must be an integer)
AB{10} - invalid (mode must be a single character)
B5 - invalid(missing brackets)

Is it possible to get both mode and value with some simple statement. I think regexp is a way to go. I just can't find exact syntax.

Question by:Rimvis
    LVL 96

    Accepted Solution

    Something like this maybe:

    using System.Text.RegularExpressions;


                string pattern = @"^(?<mode>[A-Za-z]{1}){(?<value>\d{0,})}$";
                string[] tests = { "A{1}", "B{100}", "A{0}", "A{-10}", "A{0.5}", "AB{10}", "B5" };

                foreach (string input in tests)
                    Match m = Regex.Match(input, pattern);
                    bool valid = m.Success;
                    string mode = m.Groups["mode"].Value;
                    int result = 0;
                    int.TryParse(m.Groups["value"].Value, out result);
                    valid = valid && result > 0;
                    Console.WriteLine("Test={0}, Mode={2}, Value={3}, Result={1}", input, valid, mode, result);

    Test=A{1}, Mode=A, Value=1, Result=True
    Test=B{100}, Mode=B, Value=100, Result=True
    Test=A{0}, Mode=A, Value=0, Result=False
    Test=A{-10}, Mode=, Value=0, Result=False
    Test=A{0.5}, Mode=, Value=0, Result=False
    Test=AB{10}, Mode=, Value=0, Result=False
    Test=B5, Mode=, Value=0, Result=False

    LVL 19

    Author Comment

    Thank you, TheLearnedOne,

    This is almost what I want. Your pattern allowed <value> to be empty. I changed it to this:
    Am I right?

    Also, your pattern allows value to be 0. Is it possible to avoid this?
    LVL 96

    Expert Comment

    by:Bob Learned
    The pattern may allow value to be 0, but this validation step doesn't:

       valid = valid && result > 0;

    LVL 19

    Author Comment

    Yes, of course. I'm just curious if it's possible to eliminate it in the pattern?
    Something like (?<value>(\d, but first symbol is not "0") {1,})
    LVL 96

    Expert Comment

    by:Bob Learned
    1) Regular expressions can get p retty messy

    2) I like to keep them as simple as possible

    3) You might be able to get away with this:


    LVL 19

    Author Comment

    Thank you, Bob,

    I ended up with this: @"^(?<mode>[ABC]{1}){(?<value>[1-9]\d{0,})}$"


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now