none
Regular Expression for a Version RRS feed

  • Question

  • Hi

    I am looking to construct a regular expression for the .NET Version for a string variable.

    The supported values are:

    1.2

    1.2.3

    1.2.3.4

    Each part of the version can only take upto 7 digits.

    Can any one help on this?

    Thanks

    Sahasra

    • Moved by Lisa Zhu Friday, June 7, 2013 2:57 AM Regex question
    Thursday, June 6, 2013 6:36 PM

Answers

  • Try this:

    ^\d{1,7}([.]\d{1,7}){1,3}$

    • Proposed as answer by Wyck Thursday, June 6, 2013 7:22 PM
    • Marked as answer by Mike FengModerator Thursday, June 13, 2013 12:26 AM
    Thursday, June 6, 2013 6:49 PM
  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String pattern = @"\d{1,7}\.\d{1,7}(\.\d{1,7})?(\.\d{1,7})?";
                String[] examples = { "1.2.3.4", "1.2.3", "1.2", "9086.1234568.213243", "kli.90.kl" };
                Regex rx = new Regex(pattern, RegexOptions.Compiled);
    
                foreach (String example in examples)
                {
                    if (rx.IsMatch(example))
                        Console.WriteLine("{0} has passed", example);
                    else
                        Console.WriteLine("{0} has failed", example);
                }
                Console.ReadLine();
    
            }
        }
    }
    


    Thursday, June 6, 2013 6:49 PM

All replies

  • Try this:

    ^\d{1,7}([.]\d{1,7}){1,3}$

    • Proposed as answer by Wyck Thursday, June 6, 2013 7:22 PM
    • Marked as answer by Mike FengModerator Thursday, June 13, 2013 12:26 AM
    Thursday, June 6, 2013 6:49 PM
  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String pattern = @"\d{1,7}\.\d{1,7}(\.\d{1,7})?(\.\d{1,7})?";
                String[] examples = { "1.2.3.4", "1.2.3", "1.2", "9086.1234568.213243", "kli.90.kl" };
                Regex rx = new Regex(pattern, RegexOptions.Compiled);
    
                foreach (String example in examples)
                {
                    if (rx.IsMatch(example))
                        Console.WriteLine("{0} has passed", example);
                    else
                        Console.WriteLine("{0} has failed", example);
                }
                Console.ReadLine();
    
            }
        }
    }
    


    Thursday, June 6, 2013 6:49 PM
  • I think Viorel's pattern is better, but the concept is the same.
    Thursday, June 6, 2013 6:57 PM
  • Personally I'd just use Version.TryParse which can handle all those variants.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, June 6, 2013 7:11 PM
    Moderator
  • If your are using .NET 3.5, that won't work, right?
    Thursday, June 6, 2013 7:14 PM
  • Correct.  TryParse was added in v4.  Prior to that you'd have to use Parse or the ctor wrapped in a try-catch.
    Thursday, June 6, 2013 7:31 PM
    Moderator