分享多个C#常用正则表达式的实例


本文摘自PHP中文网,作者Y2J,侵删。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

using System; 

using System.Text.RegularExpressions; 

namespace CommonTools 

/**//// <summary> 

/// RegexLib 的摘要说明。 

/// </summary> 

public class RegexLib 

//验证Email地址 

public static bool IsValidEmail(string strIn) 

// Return true if strIn is in valid e-mail format. 

return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 

//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。 

public static string MDYToDMY(String input) 

return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}"); 

//验证是否为小数 

public static bool IsValidDecimal(string strIn) 

return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]"); 

//验证是否为电话号码 

public static bool IsValidTel(string strIn) 

return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?"); 

//验证年月日 

public static bool IsValidDate(string strIn) 

return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$"); 

//验证后缀名 

public static bool IsValidPostfix(string strIn) 

return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$"); 

//验证字符是否再4至12之间 

public static bool IsValidByte(string strIn) 

return Regex.IsMatch(strIn,@"^[a-z]{4,12}$"); 

//验证IP 

public static bool IsValidIp(string strIn) 

return Regex.IsMatch(strIn,@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); 

}

以上就是分享多个C#常用正则表达式的实例的详细内容!

相关阅读 >>

详解C#winform循环播放多个视频的代码示例

如何在C#中使用bogus去创建模拟数据

详解C#winform程序的toolstripbutton自定义背景应用示例源码

关于C#中三个关键字params,ref,out的详细介绍

C#学习日记04---数据类型 之 整数类型

bitmap生成base64码(C#

具体介绍使用C#访问access数据库时,提示找不到可安装的isam(图)

C#基础入门-常量详解

C#高级编程(二)-核心C#的详解

C#高级编程(三)-对象和类型详解

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...