常用正则表达式(收集)及使用方法
2007-03-29 13:46
447 查看
电子邮件:
"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"
//日期格式yyyy/mm/dd
/^(\d{4})/(0\d{1}|1[0-2])/(0\d{1}|[12]\d{1}|3[01])$/;
"^\\d+$" //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$" //正整数
"^((-\\d+)|(0+))$" //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$" //负整数
"^-?\\d+$" //整数
"^\\d+(\\.\\d+)?$" //非负浮点数(正浮点数 + 0)
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$" //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
"^(-?\\d+)(\\.\\d+)?$" //浮点数
验证汉字:^[\u4e00-\u9fa5]+$
身份证:^(\d{18}|\d{15})$
国内电话或手机:(((\(\d{3,4}\))|(\d{3,4}-))\d{7,8})|\d{11}
------------------------------------------------------
正则表达式的日期验证
"^\\d{4}[\\-\\/\\s]?(((([13578])|(1[02]))\\-(([0-9])|([1-2][0-9])|(3[01])))|((([469])|(11))\\-(([0-9])|([1-2][0-9])|(30)))|(2\\-(([0-9])|([1-2][0-9])))) (([0]?[1-9]|1[0-2])(:)([0-5][0-9]))$"
or
^\\d{4}[\\-\\/\\s]?((((0[13578])|(1[02]))\\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\\-(([0-2][0-9])|(30)))|(02\\-[0-2][0-9])) (([0]?[1-9]|1[0-2])(:)([0-5][0-9]))$"
long or short
----------------------------------------------------
--^为匹配输入字符串开始的位置
--[^]为反向字符集--匹配未包含的任何字符。
----------------------------------------------------
正则表达式的使用方法:
// Create a new Regex object.
Regex r = new Regex("abc");
// Find a single match in the string.
Match m = r.Match("123abc456");
if (m.Success)
{
// Print out the character position where a match was found.
// (Character position 3 in this case.)
Console.WriteLine("Found match at position " + m.Index);
}
具体用法参考msdn中的相关内容
"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"
//日期格式yyyy/mm/dd
/^(\d{4})/(0\d{1}|1[0-2])/(0\d{1}|[12]\d{1}|3[01])$/;
"^\\d+$" //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$" //正整数
"^((-\\d+)|(0+))$" //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$" //负整数
"^-?\\d+$" //整数
"^\\d+(\\.\\d+)?$" //非负浮点数(正浮点数 + 0)
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$" //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
"^(-?\\d+)(\\.\\d+)?$" //浮点数
验证汉字:^[\u4e00-\u9fa5]+$
身份证:^(\d{18}|\d{15})$
国内电话或手机:(((\(\d{3,4}\))|(\d{3,4}-))\d{7,8})|\d{11}
------------------------------------------------------
正则表达式的日期验证
"^\\d{4}[\\-\\/\\s]?(((([13578])|(1[02]))\\-(([0-9])|([1-2][0-9])|(3[01])))|((([469])|(11))\\-(([0-9])|([1-2][0-9])|(30)))|(2\\-(([0-9])|([1-2][0-9])))) (([0]?[1-9]|1[0-2])(:)([0-5][0-9]))$"
or
^\\d{4}[\\-\\/\\s]?((((0[13578])|(1[02]))\\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\\-(([0-2][0-9])|(30)))|(02\\-[0-2][0-9])) (([0]?[1-9]|1[0-2])(:)([0-5][0-9]))$"
long or short
----------------------------------------------------
--^为匹配输入字符串开始的位置
--[^]为反向字符集--匹配未包含的任何字符。
----------------------------------------------------
正则表达式的使用方法:
// Create a new Regex object.
Regex r = new Regex("abc");
// Find a single match in the string.
Match m = r.Match("123abc456");
if (m.Success)
{
// Print out the character position where a match was found.
// (Character position 3 in this case.)
Console.WriteLine("Found match at position " + m.Index);
}
具体用法参考msdn中的相关内容
相关文章推荐
- C#验证控件使用方法及常用正则表达式例析
- 常用的正则表达式使用方法
- 收集常用的PHP正则表达式及使用
- 收集常用的PHP正则表达式及使用
- C#常用正则表达式验证(使用C#3.0扩展方法)(转)
- 【iOS】常用正则表达式即使用方法
- 【iOS】常用正则表达式即使用方法
- JavaScript中的String对象的常用方法、文本框对象的常用方法和事件、正则表达式的概念、正则表达式的几种构造方式、RegExp对象使用什么方法匹配正则表达式、
- 正则表达式基础知识及常用表单验证正则(使用时查找为主,需多加基础方法的例子)
- ASP.NET 常用的正则表达式(使用C#3.0扩展方法)
- Javascript RegExp 常用正则使用收集
- 正则表达式精华(包涵常用经典方法)
- 收集的ASP.NET中常用正则表达式
- C# 常用正则表达式及使用方式
- php正则表达式的使用方法
- [转载]常用正则表达式收集&勘误
- 常用正则表达式收集
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
- 项目中常用到的正则表达式(收集+共享=进步)
- ActionScript常用正则表达式收集