locked
Looking for a regex to match the following numbers RRS feed

  • Question

  • User668774724 posted

    Hi All,
    I would like t to use regex to match and get the numbers out of the following

    6.0, 6.11, 6.11.1, 6.2.1.1, 6.2.1.2, 6.2.1.3,3.2-09,1.2-09
    e.g for 6..2.1.2 I the output should be  6,2,1,2
    and for the following 3.2-09 out put should be 3,2,9

    Is there a regex I can use to match any of those the one I need help with is with the -

    Thanks in Advance

    Sunday, December 3, 2017 11:55 AM

Answers

  • User-335504541 posted

    Hi naijacoder74,

    Please try to use the following code:

                var subjectString = "3.2-09";
                var resultString = Regex.Matches(subjectString, @"\d+");
                Console.WriteLine("String is: "+subjectString);
                Console.Write("Number is:");
                string result = "";
                foreach (Match M in resultString)
                {
                    result = result + Convert.ToInt32(M.Value) + ",";                
                }
                Console.WriteLine(result.Remove(result.Length - 1));
                result = "";
                subjectString = "6.2.1.3";
                resultString = Regex.Matches(subjectString, @"\d+");
                Console.WriteLine("String is: " + subjectString);
                Console.Write("Number is:");
                foreach (Match M in resultString)
                {
                    result = result + Convert.ToInt32(M.Value) + ",";
                }
                Console.WriteLine(result.Remove(result.Length - 1));
                Console.ReadLine();

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 4, 2017 9:38 AM

All replies

  • User303363814 posted

    Why do you want a Regex? Split the string on the punctuation.

    var result = input.Split(",.- ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s=>int.Parse(s));

    Sunday, December 3, 2017 10:02 PM
  • User-335504541 posted

    Hi naijacoder74,

    Please try to use the following code:

                var subjectString = "3.2-09";
                var resultString = Regex.Matches(subjectString, @"\d+");
                Console.WriteLine("String is: "+subjectString);
                Console.Write("Number is:");
                string result = "";
                foreach (Match M in resultString)
                {
                    result = result + Convert.ToInt32(M.Value) + ",";                
                }
                Console.WriteLine(result.Remove(result.Length - 1));
                result = "";
                subjectString = "6.2.1.3";
                resultString = Regex.Matches(subjectString, @"\d+");
                Console.WriteLine("String is: " + subjectString);
                Console.Write("Number is:");
                foreach (Match M in resultString)
                {
                    result = result + Convert.ToInt32(M.Value) + ",";
                }
                Console.WriteLine(result.Remove(result.Length - 1));
                Console.ReadLine();

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 4, 2017 9:38 AM