locked
Mixed Number/Fraction Matching RRS feed

  • Question

  • Hello all,
    I am very inexperienced with Regex and am having trouble understanding some of the examples. I am currently writing a program that stores a fraction or mixed number in string format.
    Some examples include: "23 2/3" "13" "1/3"

    I would like to verify that the user has entered a valid value into the textbox.
    Any help would be much appreciated.

    Brad
    Friday, January 22, 2010 1:46 PM

Answers

  • It would be useful if you posted what you tried so far so people have a better picture of what needs to be fixed and, sometimes, provide a good starting point for others to build off of. I guess the values you mentioned are valid formats, and anything else isn't.

    Give this pattern a try: @"^(\d+/\d+|\d+(\s\d+/\d+)?)$"

    Here's a breakdown of the pattern:
    • ^ matches the start of the string
    • Next we group the rest of the pattern and specify alternate patterns using the '|' pipe symbol
    • \d+/\d+ matches the fraction format of digit/digit (ex. 1/2). \d matches a digit [0-9], the + means match it at least once
    • \d+(\s\d+/\d+)? matches a number followed by an optional space and fraction. Notice that the space and fraction are surrounded by paranethesis to group them. The group is suffixed with '?' to denote that it's optional. Making it optional means that one pattern can match an entire digit ("23") or a digit with a fraction ("23 1/3").
    • $ matches the end of the string

    To demonstrate here's a snippet:
    string[] inputs = { "23", "23 1/2", "2/3", "2.5" };
    string pattern = @"^(\d+/\d+|\d+(\s\d+/\d+)?)$";
    Regex rx = new Regex(pattern);
    
    foreach (string input in inputs)
    {
        Console.WriteLine("IsMatch: {0} : {1}", rx.IsMatch(input), input);
        
        // Using both IsMatch and Match.Success are redundant. Shown to demonstrate other options.
        Match match = rx.Match(input);
        
        if (match.Success)
        {
            Console.WriteLine("Match: {0}", match.Value);
        }
        else
        {
            Console.WriteLine("Not a match: {0}", input);
        }
    }


    Document my code? Why do you think it's called "code"?
    • Proposed as answer by SamAgain Monday, January 25, 2010 6:37 AM
    • Marked as answer by SamAgain Monday, January 25, 2010 6:38 AM
    Friday, January 22, 2010 5:18 PM

All replies

  • It would be useful if you posted what you tried so far so people have a better picture of what needs to be fixed and, sometimes, provide a good starting point for others to build off of. I guess the values you mentioned are valid formats, and anything else isn't.

    Give this pattern a try: @"^(\d+/\d+|\d+(\s\d+/\d+)?)$"

    Here's a breakdown of the pattern:
    • ^ matches the start of the string
    • Next we group the rest of the pattern and specify alternate patterns using the '|' pipe symbol
    • \d+/\d+ matches the fraction format of digit/digit (ex. 1/2). \d matches a digit [0-9], the + means match it at least once
    • \d+(\s\d+/\d+)? matches a number followed by an optional space and fraction. Notice that the space and fraction are surrounded by paranethesis to group them. The group is suffixed with '?' to denote that it's optional. Making it optional means that one pattern can match an entire digit ("23") or a digit with a fraction ("23 1/3").
    • $ matches the end of the string

    To demonstrate here's a snippet:
    string[] inputs = { "23", "23 1/2", "2/3", "2.5" };
    string pattern = @"^(\d+/\d+|\d+(\s\d+/\d+)?)$";
    Regex rx = new Regex(pattern);
    
    foreach (string input in inputs)
    {
        Console.WriteLine("IsMatch: {0} : {1}", rx.IsMatch(input), input);
        
        // Using both IsMatch and Match.Success are redundant. Shown to demonstrate other options.
        Match match = rx.Match(input);
        
        if (match.Success)
        {
            Console.WriteLine("Match: {0}", match.Value);
        }
        else
        {
            Console.WriteLine("Not a match: {0}", input);
        }
    }


    Document my code? Why do you think it's called "code"?
    • Proposed as answer by SamAgain Monday, January 25, 2010 6:37 AM
    • Marked as answer by SamAgain Monday, January 25, 2010 6:38 AM
    Friday, January 22, 2010 5:18 PM
  • It would be useful if you posted what you tried so far so people have a better picture of what needs to be fixed and, sometimes, provide a good starting point for others to build off of. I guess the values you mentioned are valid forma ts, and anything else isn't.

    Give this pattern a try: @"^(\d+/\d+|\d+(\s\d+/\d+)?)$"

    Here's a breakdown of the pattern:
    • ^ matches the start of the string
    • Next we group the rest of the pattern and specify alternate patterns using the '|' pipe symbol
    • \d+/\d+ matches the fraction format of digit/digit (ex. 1/2). \d matches a digit [0-9], the + means match it at least once
    • \d+(\s\d+/\d+)? matches a number followed by an optional space and fraction. Notice that the space and fraction are surrounded by paranethesis to group them. The group is suffixed with '?' to denote that it's optional. Making it optional means that one pattern can match an entire digit ("23") or a digit with a fraction ("23 1/3").
    • $ matches the end of the string

    To demonstrate here's a snippet:
    string[] inputs = { "23", "23 1/2", "2/3", "2.5" };string pattern = @"^(\d+/\d+|\d+(\s\d+/\d+)?)$";Regex rx = new Regex(pattern);foreach (string input in inputs){  Console.WriteLine("IsMatch: {0} : {1}", rx.IsMatch(input), input);    // Using both IsMatch and Match.Success are redundant. Shown to demonstrate other options.  Match match = rx.Match(input);    if (match.Success)  {    Console.WriteLine("Match: {0}", match.Value);  }  else  {    Console.WriteLine("Not a match: {0}", input);  }}


    Document my code? Why do you think it's called "code"?

    The example solved my annoying problem. It's really a big help for me. Thanks Ahmad.
    Wednesday, July 14, 2010 8:51 AM