正则表达式更改字符串中的第二个匹配项
regex change second match in string
Listbox1 包含以下字符串:
[随机header]随机标题[随机秒header]
[一些其他随机 header]一些其他随机标题
随机标题
[随机 header] 随机标题 [随机秒 header] (blahblah) {1234}
[一些随机header]一些随机标题[一些随机秒header]
我想保留第一个[.*]并删除第二个[.*]
希望得到如下结果:
[随机header]随机标题
[一些其他随机 header]一些其他随机标题
随机标题
[随机 header] 随机标题 (blahblah) {1234}
[一些随机的header]一些随机的标题
我无法通过以下代码得到想要的结果:
For i As Integer = 0 To Listbox1.Items.Count - 1
Listbox1.Items(i) = System.Text.RegularExpressions.Regex.Replace(Listbox1.Items(i), "\[.*\].*?(\[.*\])", "")
Next
请帮帮我
我无法用正则表达式解决它,所以我使用了 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
Listbox1 包含以下字符串:
[随机header]随机标题[随机秒header]
[一些其他随机 header]一些其他随机标题
随机标题
[随机 header] 随机标题 [随机秒 header] (blahblah) {1234}
[一些随机header]一些随机标题[一些随机秒header]
我想保留第一个[.*]并删除第二个[.*]
希望得到如下结果:
[随机header]随机标题
[一些其他随机 header]一些其他随机标题
随机标题
[随机 header] 随机标题 (blahblah) {1234}
[一些随机的header]一些随机的标题
我无法通过以下代码得到想要的结果:
For i As Integer = 0 To Listbox1.Items.Count - 1
Listbox1.Items(i) = System.Text.RegularExpressions.Regex.Replace(Listbox1.Items(i), "\[.*\].*?(\[.*\])", "")
Next
请帮帮我
我无法用正则表达式解决它,所以我使用了 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