动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 C#正则表达式应用范例

作者来源: 
阅读 2596 人次 , 2006-2-16 14:45:00 

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.
  
 本文Tags正则表达式  C#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:关于两代语言.C/C++,java/c#
· 下一篇:利用c#制作简单的留言板
· ASP.NET中Cookie编程的基础知识(4)
· 部署ASP.NET的三大技术(3)
· 使用 ADO.NET 解锁 Microsoft Access 数据(1)
· 用ASP+制作图形(有了ASP+,不需要扩展图像组件啦!)
· 10天学会ASP.net之第六天


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077