读书人

vb.net正则表达式有人能解释一下吗?

发布时间: 2012-04-02 19:58:59 作者: rapoo

vb.net正则表达式,有人能解释一下吗?

VB.NET code
Dim reg As New System.Text.RegularExpressions.Regex("w[\w]*")Dim m As System.Text.RegularExpressions.MatchCollection = reg.Matches("wire")Dim alnum As IEnumerator = m.GetEnumeratorWhile alnum.MoveNext          MsgBox(alnum.Current.groups(0).value)End While Dim reg As New System.Text.RegularExpressions.Regex("w[\w]*")        Dim m As System.Text.RegularExpressions.MatchCollection = reg.Matches("wire", System.Text.RegularExpressions.RegexOptions.IgnoreCase)        Dim alnum As IEnumerator = m.GetEnumerator        While alnum.MoveNext            MsgBox(alnum.Current.groups(0).value)        End WhileDim reg As New System.Text.RegularExpressions.Regex("[\w]*")        Dim m As System.Text.RegularExpressions.MatchCollection = reg.Matches("wire", System.Text.RegularExpressions.RegexOptions.IgnoreCase)        Dim alnum As IEnumerator = m.GetEnumerator        While alnum.MoveNext            MsgBox(alnum.Current.groups(0).value)        End While




第一个的结果是匹配的,第二个却没有,主要是加了个System.Text.RegularExpressions.RegexOptions.IgnoreCase,这不只是不区分大小写吗?第三个输出的竟然是ire,明显第一个字符不见了,难道System.Text.RegularExpressions.RegexOptions.IgnoreCase就直接忽略第一个符了?

[解决办法]
VB.NET code
        Dim m As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches("wire",                                                    "w[\w]*", System.Text.RegularExpressions.RegexOptions.IgnoreCase)        Dim alnum As IEnumerator = m.GetEnumerator        While alnum.MoveNext            MsgBox(alnum.Current.groups(0).value)        End While 

读书人网 >VB Dotnet

热点推荐