Dim str As String = "123/32131-123123 [55]" Dim r As New System.Text.RegularExpressions.Regex("(?<name>\d*)\/(?<content>\d*[-]\d*)\s*(\[(?<size>\d*)\]){0,1}") Dim mc As System.Text.RegularExpressions.MatchCollection = r.Matches(str) For i As Integer = 0 To mc.Count - 1 MsgBox(mc(i).Value) MsgBox(mc(i).Groups("name").Value) MsgBox(mc(i).Groups("content").Value) If mc(i).Groups.Count > 2 Then MsgBox(mc(i).Groups("size").Value) End If Next
可以用split()这个方法把这几个分别获取出来!!!
Dim str As String = "123/32131-123123 [55]"
Dim r As New System.Text.RegularExpressions.Regex("(?<name>\d*)\/(?<content>\d*[-]\d*)\s*(\[(?<size>\d*)\]){0,1}") Dim mc As System.Text.RegularExpressions.MatchCollection = r.Matches(str)
For i As Integer = 0 To mc.Count - 1
MsgBox(mc(i).Value)
MsgBox(mc(i).Groups("name").Value)
MsgBox(mc(i).Groups("content").Value)
If mc(i).Groups.Count > 2 Then
MsgBox(mc(i).Groups("size").Value)
End If
Next
string str = @"name/content-code [size]123/32131-123123 [55]123/32131-123123 ";
foreach (Match match in Regex.Matches(str, @"(?<name>[^\/]*)\/(?<content>[^\-]*)\-(?<code>[^\[]*)\s*\[(?<size>.*)\]|(?<name>[^\/]*)\/(?<content>[^\-]*)\-(?<code>[^\[]*)"))
{
Console.Write(match.Groups["name"].Value + " " + match.Groups["content"].Value + " " + match.Groups["code"].Value + " " + match.Groups["size"].Value);
Console.WriteLine();
}
/*输出
name content code size
123 32131 123123 55
123 32131 123123
*/