欢迎光临
我们一直在努力

c#里正则表达式的例子-.NET教程,C#语言

建站超值云服务器,限时71元/月

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

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#里正则表达式的例子-.NET教程,C#语言
分享到: 更多 (0)