C#正则表达式应用范例_C#教程
来源:互联网 发布:淘宝发布宝贝图片模糊 编辑:程序博客网 时间:2024/05/16 13:45
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.
- C#正则表达式应用范例_C#教程
- C#正则表达式应用范例_C#教程
- C#正则表达式应用范例
- C#正则表达式应用范例
- C#正则表达式范例replace
- C#正则表达式应用
- c#正则表达式应用
- C# 3.0新特性体验之Lambda表达式_C#教程
- C# 3.0新特性体验之Lambda表达式_C#教程
- 正则表达式范例-收藏
- 正则表达式-范例
- 正则表达式实用范例
- C#正则表达式应用实例
- c#正则表达式应用实例
- C# 正则表达式应用积累
- 正则表达式应用范例合集(不断更新)
- C# Timer范例教程
- C# Timer范例教程
- datagridcolumnstyle重写,实现插入不同控件列体验_C#教程
- C# 2.0与泛型_C#教程
- SUNWEN教程之----C#进阶(一)_C#教程
- C#的四个基本技巧 _C#教程
- C#基础全接触_C#教程
- C#正则表达式应用范例_C#教程
- C#中的代理(delegate)_C#教程
- SUNWEN教程之----C#进阶(三)_C#教程
- SUNWEN教程之----C#进阶(十)_C#教程
- SUNWEN教程之----C#进阶(二)_C#教程
- C#,自然的进步_C#教程
- c#学习笔记(1)_C#教程
- C#的多线程机制初探(2)_C#教程
- c#泛型学习(二)_C#教程