Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Parsing string using regular expressions

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.

  • 3
  • 3
1 Solution
Bob LearnedCommented:
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

RimvisAuthor Commented:
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?
Bob LearnedCommented:
The pattern may allow value to be 0, but this validation step doesn't:

   valid = valid && result > 0;

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

RimvisAuthor Commented:
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,})
Bob LearnedCommented:
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:


RimvisAuthor Commented:
Thank you, Bob,

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


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now