包含以下值的字符串:
Dim abc As String = 'UserId1'|'ValueA1'|'ValueB1'|'ValueC1', 'UserId2'|'ValueA2'|'ValueB2'|'ValueC2'
当前功能
Dim arrAll As String() = abc.Split(",")
Dim UserIdList As New List(Of String)
Dim ValueAList As New List(Of String)
Dim ValueBList As New List(Of String)
Dim ValueCList As New List(Of String)
For i = 0 To UBound(arrAll)
Dim arrSeparate As String() = arrAll(i).Split("|")
UserIdList.Add(arrSeparate(0))
ValueAList.Add(arrSeparate(1))
ValueBList.Add(arrSeparate(2))
ValueCList.Add(arrSeparate(3))
Next
我正在尝试将上面的值分成4个单独的列表,而不使用Split / Loop函数。
使用正则表达式,我只能检索所有的'UserId'或'ValueC'。如何获取“ ValueA”或“ ValueB”? 我对正则表达式不熟悉。任何帮助将不胜感激。
正则表达式
结果
答案 0 :(得分:0)
如果您的代码带有循环,为什么需要正则表达式?只需对您的代码进行一些调整。 Char数组中的小c告诉编译器这是一个Char而不是String。
如果您没有启用Option Strict,请立即将其打开!
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim abc As String = "'UserId1'|'ValueA1'|'ValueB1'|'ValueC1', 'UserId2'|'ValueA2'|'ValueB2'|'ValueC2'"
Dim arrAll As String() = abc.Split(","c)
Dim UserIdList As New List(Of String)
Dim ValueAList As New List(Of String)
Dim ValueBList As New List(Of String)
Dim ValueCList As New List(Of String)
'To get rid of single quote and spaces
Dim charsToTrim() As Char = {"'"c, " "c}
For i = 0 To UBound(arrAll)
Dim arrSeparate As String() = arrAll(i).Split("|"c)
UserIdList.Add(arrSeparate(0).Trim(charsToTrim))
ValueAList.Add(arrSeparate(1).Trim(charsToTrim))
ValueBList.Add(arrSeparate(2).Trim(charsToTrim))
ValueCList.Add(arrSeparate(3).Trim(charsToTrim))
Next
'Inspect the values in your lists
Debug.Print("ID List")
For Each s In UserIdList
Debug.Print(s)
Next
Debug.Print("A List")
For Each s In ValueAList
Debug.Print(s)
Next
Debug.Print("B List")
For Each s In ValueBList
Debug.Print(s)
Next
Debug.Print("C List")
For Each s In ValueCList
Debug.Print(s)
Next
End Sub
答案 1 :(得分:-1)
这应该返回所有不包含g的字符序列。或'
[^'\|]*