首页 > 开发 > 综合 > 正文

c#里正则表达式的例子

2024-07-21 02:25:40
字体:
来源:转载
供稿:网友

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);   
  }


}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表