分享多个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程序自动更新实现方法(图)

分享125个基本的C#面试问答

带你了解C#中的构造函数

C#二进制字节流查找函数indexof的示例代码详解

C#正则表达式元字符详解

C#之正则表达式介绍

C#正则函数匹配、替换、提取的用法代码分享

关于C#代码convert.tochar(null);出现异常的详解(图)

C#与vb.net混合开发测试的详细介绍(图文)

开发中常用的正则表达式

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




打赏

取消

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

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

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

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

评论

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