十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Imports System.Text.RegularExpressions
成都创新互联专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,服务器托管雅安服务有保障!
Public Class Form1
Public Function MadeRegexArray(ByVal strText As String, ByVal strRegx As String, ByVal rexOpt As RegexOptions, ByVal Groups As Integer) As String()
REM 正则结果直接以字符串组形式返回
REM strHtml待搜索的字符串
REM strRegx正则表达式
REM rexOpt正则选项
On Error Resume Next
Return MadeMatchString(MadeRegexMatch(strText, strRegx, rexOpt), Groups)
End Function
Public Function MadeMatchString(ByVal mc As MatchCollection, ByVal Groups As Integer) As String()
REM 把MatchCollection以字符串组形式保存
REM mc正则匹配的集合
REM 返回字符串组
On Error Resume Next
Dim strRegCode(mc.Count) As String
For i As Integer = 0 To mc.Count - 1
strRegCode(i) = mc(i).Groups(Groups).Value
Next i
Return strRegCode
End Function
Public Function MadeRegexMatch(ByVal strText As String, ByVal strRegex As String, ByVal rexOpt As RegexOptions) As MatchCollection
REM 获取正则表达式匹配的集合
REM strHtml待正则的字符串
REM strRegex正则表达式
REM rexOpt正则选项
REM 返回 MatchCollection 类型集合
On Error Resume Next
Dim rex As Regex = New Regex(strRegex, rexOpt)
Return rex.Matches(strText)
End Function
Public Function MadeRegexReplace(ByVal sText As String, ByVal sRegex As String, ByVal sReplace As String) As String
REM 正则表达式文本替换
REM sText原文本
REM sRegex表达式
REM sReplace替换文本
On Error Resume Next
Dim rex As Regex = New Regex(sRegex, RegexOptions.IgnoreCase)
Return rex.Replace(sText, sReplace)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error Resume Next
Dim sRegex As String = "([\s\S]*?)"
Dim aTable() As String = MadeRegexArray(TextBox1.Text, sRegex, RegexOptions.IgnoreCase, 1)
For i As Integer = 0 To aTable.Length - 1
TextBox2.Text = TextBox2.Text aTable(i)
Next i
End Sub
End Class
如图:
正则表达式是:
/[a-zA-Z/]+$
如果要把第一个斜杠去掉,就是匹配index/index/index,用后向匹配,正则是:
(?=/)[a-zA-Z/]+$
^Host:\s(.+)$
^表示是一行的开始,Host:是你要匹配的内容中固定的部分(我是这么认为的),\s表示有一个空格或者Tab,(.+)表示任意字符,就是匹配hm.baidu.com的,$表示是行的结尾,匹配结果中提取到的就是hm.baidu.com了。
MatchCollection
Dim IsMatch As Boolean = Regex,如何将SearchPattern中的指定的 那个三个小括号对应的内容即SubMatches(0)=".RegularExpressions.Groups(0);) !
For Each Mt In Matchs
On Error Resume Next
S = S Macth; Publi" SubMatches(2)=".RegularExpressions; SubMatches(1)=".Matches(",提取不出来呢;Chin".RegularExpressions.Regex(SearchPattern)
Dim Matchs As System;China Public National".+)"?
我用如下 ; 提取出来呢.Text.Regex
Regex = New System; National".IsMatch(StrForSearch)
Matchs = Regex.+)c(,执行了此句之后;(.Text.Text.+)a(;
Dim Regex As System比如正则表达式为
SearchPattern=",具体解决方案如下:
解决方案1:
在这之后的字符会显示不出来.RegularExpressions.Regex(SearchPattern)
Dim Matchs As System;Groups集合索引从0开始.Item(i),但是第一项是完整匹配项,这个是字符串的结束标记.+)c(,接下去才是子项
S = S .Text;"Dim IsMatch As Boolean = Regex.RegularExpressions,但是不要用Chr(0).Regex
Regex = New System;China Public National".RegularExpressions.Value vbcrlf
Next
也是可以的;
With Matchs; ".Count - 1 '.Text.MatchCollection
'.+)".IsMatch(StrForSearch)
Matchs = Regex:
For Each Mt In Matchs.groups
S = S Mt.Item(0);
Next
End With
MsgBox(S)
你的循环改成.Matches(")
Dim S As String = "
Dim Regex As System.Item(0).Value "Dim SearchPattern As String = ".Text.Groups
For i As Integer = 1 To ;(.+)a(
提问者评价
多谢指教!
解决方案2:
Dim SearchPattern As String = "(.+)a(.+)c(.+)"
Dim Regex As System.Text.RegularExpressions.Regex
Regex = New System.Text.RegularExpressions.Regex(SearchPattern)
Dim Matchs As System.Text.RegularExpressions.MatchCollection
'Dim IsMatch As Boolean = Regex.IsMatch(StrForSearch)
Matchs = Regex.Matches("China Public National")
Dim S As String = ""
With Matchs.Item(0).Groups
For i As Integer = 1 To .Count - 1 'Groups集合索引从0开始,但是第一项是完整匹配项,接下去才是子项
S = S .Item(i).Value " "
Next
End With
MsgBox(S)
解决方案3:
Dim matches As MatchCollection = rx.Matches("(.*)")
合法性校验有很多方法,要视情况而定,比如说可以用正则表达式,如果是数字,除了正则表达式,也可以用诸如TryParse这种方法。