locked
Regex match a number between 100 and 200 and no letters

    Question

  • Hello i`m new in regex and i relly need some help how to fin a solution in this problem

    match a number between 100 and 200 inclusive and no letters allowed

    Thx in advance hope some one will help me with this

    Best regards

     

    Tuesday, September 28, 2010 11:55 AM

Answers

  • Hi,

    to find numbers in a defined range, you can use a pattern like

    1[0-9]{2}|200

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:34 AM
    Tuesday, September 28, 2010 12:05 PM
  • Are you using Visual Studio?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Int32[] numbers = { 10, 200, 201, 129, 100, 90 };
          foreach (Int32 num in numbers)
          {
            if (IsLegit(num))
              Console.WriteLine("{0} is between 100 - 200", num);
            else
              Console.WriteLine("{0} is NOT between 100 - 200", num);
          }
          Console.ReadLine();
        }
    
        /// <summary>
        /// Matches between 100 - 200
        /// </summary>
        /// <param name="number">Number to be checked</param>
        /// <returns>Boolean</returns>
        static Boolean IsLegit(Int32 number)
        {
          Boolean result = false;
          if (number <= 200 && number >= 100)
            result = true;
          return result;      
        }
      }
    }
    


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:34 AM
    Tuesday, September 28, 2010 5:23 PM
  • Or if you have strings:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Globalization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          String[] numbers = { "10", "200", "201", "129", "100sa", "90ajk" };      
          foreach (String num in numbers)
          {
            Int32 n;
            if (Int32.TryParse(num, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out n))
            {
              if (n >= 100 && n <= 200)
                Console.WriteLine("{0} is between 100-200", n);
              else
                Console.WriteLine("{0} is NOT between 100-200", n);
            }
            else
            {
              Console.WriteLine("{0} has illegal characters", num);
            }
          }      
          Console.ReadLine();
        }    
      }
    }
    
    
    

     

     


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:33 AM
    Tuesday, September 28, 2010 5:39 PM

All replies

  • Hi,

    to find numbers in a defined range, you can use a pattern like

    1[0-9]{2}|200

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:34 AM
    Tuesday, September 28, 2010 12:05 PM
  • Are you using Visual Studio?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Int32[] numbers = { 10, 200, 201, 129, 100, 90 };
          foreach (Int32 num in numbers)
          {
            if (IsLegit(num))
              Console.WriteLine("{0} is between 100 - 200", num);
            else
              Console.WriteLine("{0} is NOT between 100 - 200", num);
          }
          Console.ReadLine();
        }
    
        /// <summary>
        /// Matches between 100 - 200
        /// </summary>
        /// <param name="number">Number to be checked</param>
        /// <returns>Boolean</returns>
        static Boolean IsLegit(Int32 number)
        {
          Boolean result = false;
          if (number <= 200 && number >= 100)
            result = true;
          return result;      
        }
      }
    }
    


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:34 AM
    Tuesday, September 28, 2010 5:23 PM
  • Or if you have strings:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Globalization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          String[] numbers = { "10", "200", "201", "129", "100sa", "90ajk" };      
          foreach (String num in numbers)
          {
            Int32 n;
            if (Int32.TryParse(num, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out n))
            {
              if (n >= 100 && n <= 200)
                Console.WriteLine("{0} is between 100-200", n);
              else
                Console.WriteLine("{0} is NOT between 100-200", n);
            }
            else
            {
              Console.WriteLine("{0} has illegal characters", num);
            }
          }      
          Console.ReadLine();
        }    
      }
    }
    
    
    

     

     


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:33 AM
    Tuesday, September 28, 2010 5:39 PM
  • Thx for all your quick respond

    i dont use visual s i need it for ruby ...

    Thx a lot one more tine

    Wednesday, September 29, 2010 7:46 AM