国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

正則表達式應用范例(轉)

2024-07-21 02:20:22
字體:
來源:轉載
供稿:網友
the following example shows the use of regular expresssions in c#.this program has basic validation scripts for validation easily useable in all programs.


/*
<howtocompile>
  csc /r:system.text.regularexpressions.dll,system.dll validation.cs
</howtocomplie>
*/
  

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


}


there is another simple way to perform these validation think of it while the next article comes.

中國最大的web開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌什县| 新巴尔虎左旗| 邮箱| 麟游县| 隆昌县| 辽阳县| 潍坊市| 乐山市| 田东县| 博兴县| 广饶县| 虎林市| 蚌埠市| 乌兰浩特市| 永新县| 桐乡市| 娄底市| 育儿| 漯河市| 尤溪县| 杭锦后旗| 亳州市| 治县。| 曲阳县| 永福县| 收藏| 彭阳县| 海阳市| 黔南| 霍林郭勒市| 饶平县| 巴青县| 平昌县| 五指山市| 越西县| 衡南县| 清苑县| 烟台市| 乐至县| 运城市| 龙岩市|