正则表达式的一个简单例子
2008-01-21 23:52
267 查看
/**//*
csc /r:System.Text.RegularExpressions.dll,System.dll Validation.cs
*/
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);
}
}
相关文章推荐
- 一个最简单的例子,清楚说明回调函数的作用和强大功能
- 使用Java开发一个非常简单的Web Service例子
- 一个简单的hibernate连接oracle数据库例子
- directx的简单例子(打开一个d3d窗口)注意的地方
- Go调用Java的一个简单例子
- SVM实现多分类的程序基础工作(二)——通过一个简单libsvm例子迈入libsvm学习的大门
- SQL Server一个表添加数据,引发另一个表更新数据 的一个简单触发器例子
- js 排列 组合 的一个简单例子
- Spring 入门(一个简单的例子)
- 一个简单的小例子让你明白c#中的委托-终于懂了!
- 一个超简单的akka actor例子
- 一个LDAP验证的简单例子
- 一个简单的oracle函数返回数组的例子[zz]
- 一个简单正则匹配的例子
- perl 一个简单的面向对象的例子
- 一个简单的Android Library Project例子
- Kotlin开发android:一个简单的登陆例子
- 想寻找一个简单的将图片插入数据库的例子~
- 通过一个简单的例子,说明class文件在运行过程中,import中class怎么被加载并执行的