首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

求教正则表达式的循环读取有关问题

2012-03-09 
求教正则表达式的循环读取问题假设字符串A$内容A%3242$内容B%34435$内容C%......然后用正则表达式将$和

求教正则表达式的循环读取问题
假设字符串A="$内容A%3242$内容B%34435$内容C%......"
然后用正则表达式将$和%符号间的内容循环读取出来得到:
内容A
内容B
内容C

目前有正则函数

VB.NET code
 Public Function Extract(ByVal strMsg As String, ByVal startStr As String, ByVal endStr As String, ByVal Rete As Boolean) As String        Dim rgx As Regex = New Regex("(?is)(?<=" & startStr & ").*?(?=" & endStr & ")")        If Rete Then            Return startStr & rgx.Match(strMsg).Value & endStr        Else            Return rgx.Match(strMsg).Value        End IfEnd Function

用Extract(A,"$","%", False) 可以得到内容A
请问怎么把后面数据循环读取出来?

字符串A是HTML格式,可能有特殊字符,利用数组Split方法不行,会出错得不到结果。

[解决办法]
VB.NET code
Public Sub Extract(ByVal strMsg As String, ByVal startStr As String, ByVal endStr As String, ByVal Rete As Boolean) As String        Dim rgx As Regex = New Regex("(?is)(?<=" & startStr & ").*?(?=" & endStr & ")")           '这个foreach是C#语法,提供思路        foreach(Match m in rgx.Matches(strMsg))        {             If Rete Then           Console.WriteLine(startStr & rgx.Match(strMsg).Value & endStr)        Else             Console.WriteLine(rgx.Match(strMsg).Value)        End If         }      End Function 

热点排行