要求:解析字符串并取值
Dim str AsString = "[11k中国f334][22dfsk文字f3][3333][44ffffff]"
① 确认字符串的格式为四个连续的"[]"组成
② 格式正确的话,取"[]"中间的值
Imports System.Text.RegularExpressions ''' ''' 取以某字符开始,以某字符结束的字符串(正则表达式:VB.net) ''' ''' Module Module1 Sub Main() 'String to Check Dim str As String = "[11k中国f334][22dfsk文字f3][3333][44ffffff]" 'Check Format Dim mcFormatCheck As MatchCollection = Regex.Matches(str, "((\[)([^\[\]]+)(\])){4}") 'if Match If mcFormatCheck.Count = 1 Then 'Get the content between [ and ] Dim mc As MatchCollection = Regex.Matches(str, "((\[)(.+?)(\]))") For i As Integer = 0 To mc.Count - 1 'Output the items whick is matched Console.WriteLine(i.ToString() + ": " + mc.Item(i).Value) Next End If Dim c As String = Console.ReadLine() End Sub End Module
Dim str AsString = "[11k中国f334][22dfsk文字f3][3333][44ffffff]"
① 确认字符串的格式为四个连续的"[]"组成
② 格式正确的话,取"[]"中间的值
Imports System.Text.RegularExpressions ''' ''' 取以某字符开始,以某字符结束的字符串(正则表达式:VB.net) ''' ''' Module Module1 Sub Main() 'String to Check Dim str As String = "[11k中国f334][22dfsk文字f3][3333][44ffffff]" 'Check Format Dim mcFormatCheck As MatchCollection = Regex.Matches(str, "((\[)([^\[\]]+)(\])){4}") 'if Match If mcFormatCheck.Count = 1 Then 'Get the content between [ and ] Dim mc As MatchCollection = Regex.Matches(str, "((\[)(.+?)(\]))") For i As Integer = 0 To mc.Count - 1 'Output the items whick is matched Console.WriteLine(i.ToString() + ": " + mc.Item(i).Value) Next End If Dim c As String = Console.ReadLine() End Sub End Module