2015-01-08 32 views
0

ListBox1中包含以下字符串:正则表达式改变第二匹配字符串

[随机报头]随机标题[随机第二报头]

[一些其他随机头]一些其他的随机标题

随机标题

[随机标题]随机标题[随机第二标题](blahblah){1234}

[一些随机的头]一些随机的标题[一些随机的第二头]

我要离开第一[*]并删除第二个[*]

希望得到如下结果:

[随机头]随机标题

[其他一些随机标题]其他一些随机标题

随机

标题

[随机报头]随机标题(blahblah){1234}

[一些随机报头]一些随机标题

我无法得到与所希望的结果下面的代码:

For i As Integer = 0 To Listbox1.Items.Count - 1 
Listbox1.Items(i) = System.Text.RegularExpressions.Regex.Replace(Listbox1.Items(i), "\[.*\].*?(\[.*\])", "") 
    Next 

请帮我

回答

0

我不能用正则表达式解决它,所以我用了indexof方法。

For i As Integer = 0 To listbox1.Items.Count - 1 

try 
Dim M1 As Integer = listbox1.Items(i).IndexOf("(") 
Dim N1 As Integer = listbox1.Items(i).IndexOf("(", M1 + 2) 

Dim M2 As Integer = listbox1.Items(i).IndexOf(")") 
Dim N2 As Integer = listbox1.Items(i).IndexOf(")", M2 + 2) 

Dim bw1 As String = listbox1.Items(i).Substring(N1, N2 - N1 + 1) 
listbox1.Items(i) = listbox1.Items(i).ToString.Replace(bw1.ToString, "") 
    Catch ex As Exception 
      End Try 

next