C#正则表达式应用范例

 

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]+|[09]+");

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]+|[09][.][09]+");

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.

 

posted @   古道轻风  阅读(279)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示