c#裡Regex的例子

來源:互聯網
上載者:User
正則 using System.Text.RegularExpressions;
using System;


class Validation
{
  public static void Main()
  {
    String strToTest;
    Validation objValidate=new Validation();

    Console.Write("Enter a String to Test for Alphabets:");
    strToTest=Console.ReadLine();
    if(objValidate.IsAlpha(strToTest))
    {
      Console.WriteLine("{0} is Valid Alpha String",strToTest);
    }
    else
    {
      Console.WriteLine("{0} is not a Valid Alpha String",strToTest);
    }
  }

  // Function to test for Positive Integers.

  public bool IsNaturalNumber(String strNumber)
  {
    Regex objNotNaturalPattern=new Regex("[^0-9]");
    Regex objNaturalPattern=new Regex("0*[1-9][0-9]*");

    return  !objNotNaturalPattern.IsMatch(strNumber) &&
            objNaturalPattern.IsMatch(strNumber);
  }

  // Function to test for Positive Integers with zero inclusive

    public bool IsWholeNumber(String strNumber)
    {
      Regex objNotWholePattern=new Regex("[^0-9]");

      return !objNotWholePattern.IsMatch(strNumber);
    }

  // Function to Test for Integers both Positive & Negative

    public bool IsInteger(String strNumber)
    {
      Regex objNotIntPattern=new Regex("[^0-9-]");
      Regex objIntPattern=new Regex("^-[0-9]+$|^[0-9]+$");

      return  !objNotIntPattern.IsMatch(strNumber) &&
              objIntPattern.IsMatch(strNumber);
    }

  // Function to Test for Positive Number both Integer & Real

  public bool IsPositiveNumber(String strNumber)
  {
    Regex objNotPositivePattern=new Regex("[^0-9.]");
    Regex objPositivePattern=new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
    Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");

    return !objNotPositivePattern.IsMatch(strNumber) &&
           objPositivePattern.IsMatch(strNumber)  &&
           !objTwoDotPattern.IsMatch(strNumber);
  }

  // Function to test whether the string is valid number or not
  public bool IsNumber(String strNumber)
  {
    Regex objNotNumberPattern=new Regex("[^0-9.-]");
    Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
    Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
    String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
    String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
    Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");

    return !objNotNumberPattern.IsMatch(strNumber) &&
           !objTwoDotPattern.IsMatch(strNumber) &&
           !objTwoMinusPattern.IsMatch(strNumber) &&
           objNumberPattern.IsMatch(strNumber);
  }

  // Function To test for Alphabets.

  public bool IsAlpha(String strToCheck)
  {
    Regex objAlphaPattern=new Regex("[^a-zA-Z]");

    return !objAlphaPattern.IsMatch(strToCheck);
  }

  // Function to Check for AlphaNumeric.

  public bool IsAlphaNumeric(String strToCheck)
  {
    Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]");

    return !objAlphaNumericPattern.IsMatch(strToCheck);   
  }


}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.