正则表达式处理基类
发个出来.希望对做项目的有些帮助.
1Imports System
2Imports System.Text
3Imports System.Text.RegularExpressions
4
5Namespace WanerSoft.Provider.Text
6
7 ''' <summary>
8 ''' 正则表达式处理
9 ''' </summary>
10 ''' <remarks></remarks>
11 Public Class RegExp
12
13 ''' <summary>
14 ''' 正则表达式替换,重写
15 ''' </summary>
16 ''' <param name="input">输入内容</param>
17 ''' <param name="pattern">替换规则</param>
18 ''' <param name="replacement">替换结果</param>
19 ''' <returns></returns>
20 ''' <remarks></remarks>
21 Public Shared Function Replace(ByVal input As String, ByVal pattern As String, ByVal replacement As String) As String
22 Return Regex.Replace(input, pattern, replacement, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
23 End Function
24
25 ''' <summary>
26 ''' 正则表达式替换,重写
27 ''' </summary>
28 ''' <param name="input">输入内容</param>
29 ''' <param name="pattern">替换规则</param>
30 ''' <param name="replacement">替换结果</param>
31 ''' <param name="regexOptions">RegexOptions枚举</param>
32 ''' <returns></returns>
33 ''' <remarks></remarks>
34 Public Shared Function Replace(ByVal input As String, ByVal pattern As String, ByVal replacement As String, ByVal regexOptions As RegexOptions) As String
35 Return Regex.Replace(input, pattern, replacement, regexOptions)
36 End Function
37
38 ''' <summary>
39 ''' 正则表达式替换,批量,重写
40 ''' </summary>
41 ''' <param name="input">输入内容</param>
42 ''' <param name="pattern">替换规则</param>
43 ''' <param name="replacement">替换结果</param>
44 ''' <param name="ro">RegexOptions枚举</param>
45 ''' <returns></returns>
46 ''' <remarks></remarks>
47 Public Shared Function Replace(ByVal input As String, ByVal pattern As String(), ByVal replacement As String(), ByVal ro As RegexOptions) As String
48 For i As Integer = 0 To pattern.Length - 1
49 input = Regex.Replace(input, pattern(i), replacement(i), ro)
50 Next
51 Return input
52 End Function
53
54 ''' <summary>
55 ''' 搜索正则表达式的所有匹配项,不区分大小写及换行
56 ''' </summary>
57 ''' <param name="input">搜索内容</param>
58 ''' <param name="pattern">匹配规则</param>
59 ''' <returns></returns>
60 ''' <remarks></remarks>
61 Public Shared Function Matches(ByVal input As String, ByVal pattern As String) As MatchCollection
62 Dim ObjRegex As Regex = New Regex(pattern, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
63 Return ObjRegex.Matches(input)
64 End Function
65
66 ''' <summary>
67 ''' 单个正则表达式匹配的结果,不区分大小写及换行
68 ''' </summary>
69 ''' <param name="input">搜索内容</param>
70 ''' <param name="pattern">匹配规则</param>
71 ''' <returns></returns>
72 ''' <remarks></remarks>
73 Public Shared Function Match(ByVal input As String, ByVal pattern As String) As Match
74 Dim ObjRegex As Regex = New Regex(pattern, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
75 Return ObjRegex.Match(input)
76 End Function
77
78 ''' <summary>
79 ''' 判断是否为物理路径
80 ''' </summary>
81 ''' <param name="Path">地址</param>
82 ''' <returns></returns>
83 ''' <remarks></remarks>
84 Public Shared Function IsPhysicalPath(ByVal Path As String) As Boolean
85 Dim pattern As String = "^\s*[a-zA-Z]:.*$"
86 Return Regex.IsMatch(Path, pattern)
87 End Function
88
89 ''' <summary>
90 ''' 判断是否为相对路径
91 ''' </summary>
92 ''' <param name="s">地址</param>
93 ''' <returns></returns>
94 ''' <remarks></remarks>
95 Public Shared Function IsRelativePath(ByVal s As String) As Boolean
96 If ((s Is Nothing) OrElse (s = String.Empty)) Then
97 Return False
98 End If
99 If (s.StartsWith("/") OrElse s.StartsWith("?")) Then
100 Return False
101 End If
102 If Regex.IsMatch(s, "^\s*[a-zA-Z]{1,10}:.*$") Then
103 Return False
104 End If
105 Return True
106 End Function
107
108 ''' <summary>
109 ''' 判断是否为邮件地址
110 ''' </summary>
111 ''' <param name="s">内容</param>
112 ''' <returns></returns>
113 ''' <remarks></remarks>
114 Public Shared Function IsEmail(ByVal s As String) As Boolean
115 Dim pattern As String = "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"
116 Return Regex.IsMatch(s, pattern)
117 End Function
118
119 ''' <summary>
120 ''' 判断是否为IP
121 ''' </summary>
122 ''' <param name="s">内容</param>
123 ''' <returns></returns>
124 ''' <remarks></remarks>
125 Public Shared Function IsIp(ByVal s As String) As Boolean
126 Dim pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
127 Return Regex.IsMatch(s, pattern)
128 End Function
129
130 ''' <summary>
131 ''' 判断是否为数字
132 ''' </summary>
133 ''' <param name="s">内容</param>
134 ''' <returns></returns>
135 ''' <remarks></remarks>
136 Public Shared Function IsNumeric(ByVal s As String) As Boolean
137 Dim pattern As String = "^\-?[0-9]+$"
138 Return Regex.IsMatch(s, pattern)
139 End Function
140
141 ''' <summary>
142 ''' 判断是否为整数类型
143 ''' </summary>
144 ''' <param name="s">内容</param>
145 ''' <returns></returns>
146 ''' <remarks></remarks>
147 Public Shared Function IsUnsNumeric(ByVal s As String) As Boolean
148 Dim pattern As String = "^[0-9]+$"
149 Return Regex.IsMatch(s, pattern)
150 End Function
151
152 ''' <summary>
153 ''' 判断是否为小数
154 ''' </summary>
155 ''' <param name="s">内容</param>
156 ''' <returns></returns>
157 ''' <remarks></remarks>
158 Public Shared Function IsUnsFlaot(ByVal s As String) As Boolean
159 Dim pattern As String = "^[0-9]+.?[0-9]+$"
160 Return Regex.IsMatch(s, pattern)
161 End Function
162
163 ''' <summary>
164 ''' 判断是否为安全字符串(防注入)
165 ''' </summary>
166 ''' <param name="s">内容</param>
167 ''' <returns></returns>
168 ''' <remarks></remarks>
169 Public Shared Function IsSafety(ByVal s As String) As Boolean
170 Dim input As String = Regex.Replace(s.Replace("%20", " "), "\s", " ")
171 Dim pattern As String = "select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|""|\'| or "
172 Return Not Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase)
173 End Function
174
175 ''' <summary>
176 ''' 判断是否为Unicode
177 ''' </summary>
178 ''' <param name="s">内容</param>
179 ''' <returns></returns>
180 ''' <remarks></remarks>
181 Public Shared Function IsUnicode(ByVal s As String) As Boolean
182 Dim pattern As String = "^[\u4E00-\u9FA5\uE815-\uFA29]+$"
183 Return Regex.IsMatch(s, pattern)
184 End Function
185
186 ''' <summary>
187 ''' 判断是否为Url
188 ''' </summary>
189 ''' <param name="s">内容</param>
190 ''' <returns></returns>
191 ''' <remarks></remarks>
192 Public Shared Function IsUrl(ByVal s As String) As Boolean
193 Dim pattern As String = "^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"
194 Return Regex.IsMatch(s, pattern, RegexOptions.IgnoreCase)
195 End Function
196
197 End Class
198
199End Namespace
200
2Imports System.Text
3Imports System.Text.RegularExpressions
4
5Namespace WanerSoft.Provider.Text
6
7 ''' <summary>
8 ''' 正则表达式处理
9 ''' </summary>
10 ''' <remarks></remarks>
11 Public Class RegExp
12
13 ''' <summary>
14 ''' 正则表达式替换,重写
15 ''' </summary>
16 ''' <param name="input">输入内容</param>
17 ''' <param name="pattern">替换规则</param>
18 ''' <param name="replacement">替换结果</param>
19 ''' <returns></returns>
20 ''' <remarks></remarks>
21 Public Shared Function Replace(ByVal input As String, ByVal pattern As String, ByVal replacement As String) As String
22 Return Regex.Replace(input, pattern, replacement, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
23 End Function
24
25 ''' <summary>
26 ''' 正则表达式替换,重写
27 ''' </summary>
28 ''' <param name="input">输入内容</param>
29 ''' <param name="pattern">替换规则</param>
30 ''' <param name="replacement">替换结果</param>
31 ''' <param name="regexOptions">RegexOptions枚举</param>
32 ''' <returns></returns>
33 ''' <remarks></remarks>
34 Public Shared Function Replace(ByVal input As String, ByVal pattern As String, ByVal replacement As String, ByVal regexOptions As RegexOptions) As String
35 Return Regex.Replace(input, pattern, replacement, regexOptions)
36 End Function
37
38 ''' <summary>
39 ''' 正则表达式替换,批量,重写
40 ''' </summary>
41 ''' <param name="input">输入内容</param>
42 ''' <param name="pattern">替换规则</param>
43 ''' <param name="replacement">替换结果</param>
44 ''' <param name="ro">RegexOptions枚举</param>
45 ''' <returns></returns>
46 ''' <remarks></remarks>
47 Public Shared Function Replace(ByVal input As String, ByVal pattern As String(), ByVal replacement As String(), ByVal ro As RegexOptions) As String
48 For i As Integer = 0 To pattern.Length - 1
49 input = Regex.Replace(input, pattern(i), replacement(i), ro)
50 Next
51 Return input
52 End Function
53
54 ''' <summary>
55 ''' 搜索正则表达式的所有匹配项,不区分大小写及换行
56 ''' </summary>
57 ''' <param name="input">搜索内容</param>
58 ''' <param name="pattern">匹配规则</param>
59 ''' <returns></returns>
60 ''' <remarks></remarks>
61 Public Shared Function Matches(ByVal input As String, ByVal pattern As String) As MatchCollection
62 Dim ObjRegex As Regex = New Regex(pattern, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
63 Return ObjRegex.Matches(input)
64 End Function
65
66 ''' <summary>
67 ''' 单个正则表达式匹配的结果,不区分大小写及换行
68 ''' </summary>
69 ''' <param name="input">搜索内容</param>
70 ''' <param name="pattern">匹配规则</param>
71 ''' <returns></returns>
72 ''' <remarks></remarks>
73 Public Shared Function Match(ByVal input As String, ByVal pattern As String) As Match
74 Dim ObjRegex As Regex = New Regex(pattern, (RegexOptions.Multiline Or RegexOptions.IgnoreCase))
75 Return ObjRegex.Match(input)
76 End Function
77
78 ''' <summary>
79 ''' 判断是否为物理路径
80 ''' </summary>
81 ''' <param name="Path">地址</param>
82 ''' <returns></returns>
83 ''' <remarks></remarks>
84 Public Shared Function IsPhysicalPath(ByVal Path As String) As Boolean
85 Dim pattern As String = "^\s*[a-zA-Z]:.*$"
86 Return Regex.IsMatch(Path, pattern)
87 End Function
88
89 ''' <summary>
90 ''' 判断是否为相对路径
91 ''' </summary>
92 ''' <param name="s">地址</param>
93 ''' <returns></returns>
94 ''' <remarks></remarks>
95 Public Shared Function IsRelativePath(ByVal s As String) As Boolean
96 If ((s Is Nothing) OrElse (s = String.Empty)) Then
97 Return False
98 End If
99 If (s.StartsWith("/") OrElse s.StartsWith("?")) Then
100 Return False
101 End If
102 If Regex.IsMatch(s, "^\s*[a-zA-Z]{1,10}:.*$") Then
103 Return False
104 End If
105 Return True
106 End Function
107
108 ''' <summary>
109 ''' 判断是否为邮件地址
110 ''' </summary>
111 ''' <param name="s">内容</param>
112 ''' <returns></returns>
113 ''' <remarks></remarks>
114 Public Shared Function IsEmail(ByVal s As String) As Boolean
115 Dim pattern As String = "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"
116 Return Regex.IsMatch(s, pattern)
117 End Function
118
119 ''' <summary>
120 ''' 判断是否为IP
121 ''' </summary>
122 ''' <param name="s">内容</param>
123 ''' <returns></returns>
124 ''' <remarks></remarks>
125 Public Shared Function IsIp(ByVal s As String) As Boolean
126 Dim pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
127 Return Regex.IsMatch(s, pattern)
128 End Function
129
130 ''' <summary>
131 ''' 判断是否为数字
132 ''' </summary>
133 ''' <param name="s">内容</param>
134 ''' <returns></returns>
135 ''' <remarks></remarks>
136 Public Shared Function IsNumeric(ByVal s As String) As Boolean
137 Dim pattern As String = "^\-?[0-9]+$"
138 Return Regex.IsMatch(s, pattern)
139 End Function
140
141 ''' <summary>
142 ''' 判断是否为整数类型
143 ''' </summary>
144 ''' <param name="s">内容</param>
145 ''' <returns></returns>
146 ''' <remarks></remarks>
147 Public Shared Function IsUnsNumeric(ByVal s As String) As Boolean
148 Dim pattern As String = "^[0-9]+$"
149 Return Regex.IsMatch(s, pattern)
150 End Function
151
152 ''' <summary>
153 ''' 判断是否为小数
154 ''' </summary>
155 ''' <param name="s">内容</param>
156 ''' <returns></returns>
157 ''' <remarks></remarks>
158 Public Shared Function IsUnsFlaot(ByVal s As String) As Boolean
159 Dim pattern As String = "^[0-9]+.?[0-9]+$"
160 Return Regex.IsMatch(s, pattern)
161 End Function
162
163 ''' <summary>
164 ''' 判断是否为安全字符串(防注入)
165 ''' </summary>
166 ''' <param name="s">内容</param>
167 ''' <returns></returns>
168 ''' <remarks></remarks>
169 Public Shared Function IsSafety(ByVal s As String) As Boolean
170 Dim input As String = Regex.Replace(s.Replace("%20", " "), "\s", " ")
171 Dim pattern As String = "select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|""|\'| or "
172 Return Not Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase)
173 End Function
174
175 ''' <summary>
176 ''' 判断是否为Unicode
177 ''' </summary>
178 ''' <param name="s">内容</param>
179 ''' <returns></returns>
180 ''' <remarks></remarks>
181 Public Shared Function IsUnicode(ByVal s As String) As Boolean
182 Dim pattern As String = "^[\u4E00-\u9FA5\uE815-\uFA29]+$"
183 Return Regex.IsMatch(s, pattern)
184 End Function
185
186 ''' <summary>
187 ''' 判断是否为Url
188 ''' </summary>
189 ''' <param name="s">内容</param>
190 ''' <returns></returns>
191 ''' <remarks></remarks>
192 Public Shared Function IsUrl(ByVal s As String) As Boolean
193 Dim pattern As String = "^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"
194 Return Regex.IsMatch(s, pattern, RegexOptions.IgnoreCase)
195 End Function
196
197 End Class
198
199End Namespace
200