如何将此字符串RegEx到列表中?
我的绳子是这样的:
google (2.0.3) - Python bindings to the Google search engine.
bits-google (1.12.17) - BITS Google
oauthkit-google (0.1.2) - OAuthKit for Google
google-reauth (0.1.0) - Google Reauth Library
google-common (0.0.1) - Google namespace package
google-colab (1.0.0) - Google Colaboratory tools
google-auth (1.11.2) - Google Authentication Library
INSTALLED: 1.7.0
LATEST: 1.11.2
google-endpoints (4.8.0) - Google Cloud Endpoints
google-oauth (1.0.1) - OAuth2 for Google APIs
google-gax (0.16.0) - Google API Extensions
google-finance (0.1.0) - Google Finance API ikmgitj oijhotrjtrhj
kjhghjihotrhjtri9h我想得到name,version,description,以及最终是否安装了它。
我知道如何用一个空格分隔字符串,但是如果字符串有多个不同的空格,并且其中有一个破折号,我该如何做呢?
编辑:
我现在像这样集成了代码(对于那些感兴趣的人来说)。信用):
Dim patt = "(.*?.)(\(\d+.\d+.\d+\)|\d+.\d+.\d+)(=?.*?-.)?(.*)"
Dim matches = Regex.Matches(sOutput, patt, RegexOptions.IgnoreCase)
Dim found_packages As New Dictionary(Of String, List(Of String))
For Each m As Match In matches
Dim key = m.Groups(1).Value.Trim({" "c, ":"c})
If key = "INSTALLED" Then
found_packages(found_packages.Last().Key).Add(m.Groups(2).Value.Trim({" "c, ":"c}))
Continue For
ElseIf key = "LATEST" Then
Continue For
End If
found_packages.Add(key, New List(Of String))
found_packages(key).Add(m.Groups(2).Value.Trim({" "c, ":"c}).Replace("(", "").Replace(")", ""))
If Not String.IsNullOrWhiteSpace(m.Groups(4).Value) Then
found_packages(key).Add(m.Groups(4).Value.Trim({" "c, ":"c}))
End If
Next
For Each package In found_packages
Dim item As New ListViewItem
item.Text = package.Key
item.SubItems.Add(package.Value(0))
item.SubItems.Add(package.Value(1))
If package.Value.Count > 2 Then
item.SubItems.Add(package.Value(2))
End If
lvPackages.Items.Add(item)
Next发布于 2020-02-23 22:08:05
您可以使用RegEx解析此输入并创建一个Dictionary(Of String, List(Of String)),其中每个键值条目都是一个具有多个值( version和description)的属性( name):
Dim patt = "(.*?.)(\(\d+.\d+.\d+\)|\d+.\d+.\d+)(=?.*?-.)?(.*)"
Dim matches = Regex.Matches(txtIn.Text, patt)
Dim dict As New Dictionary(Of String, List(Of String))
For Each m As Match In matches
Dim key = m.Groups(1).Value.Trim({" "c, ":"c})
dict.Add(key, New List(Of String))
dict(key).Add(m.Groups(2).Value.Trim({" "c, ":"c}))
If Not String.IsNullOrWhiteSpace(m.Groups(4).Value) Then
dict(key).Add(m.Groups(4).Value.Trim({" "c, ":"c}))
End If
Nextdict包含以下内容:
google
(2.0.3)
Python bindings to the Google search engine.
bits-google
(1.12.17)
BITS Google
oauthkit-google
(0.1.2)
OAuthKit for Google
google-reauth
(0.1.0)
Google Reauth Library
google-common
(0.0.1)
Google namespace package
google-colab
(1.0.0)
Google Colaboratory tools
google-auth
(1.11.2)
Google Authentication Library
INSTALLED
1.7.0
LATEST
1.11.2
google-endpoints
(4.8.0)
Google Cloud Endpoints
google-oauth
(1.0.1)
OAuth2 for Google APIs
google-gax
(0.16.0)
Google API Extensions
google-finance
(0.1.0)
Google Finance API ikmgitj oijhotrjtrhj kjhghjihotrhjtri9h例如,您可以获得google键(name)的值(google和description),如下所示:
Dim version = dict("google")(0)
Dim descrip = dict("google")(1)注意,INSTALLED和LATEST键都没有description值,因此Dim descrip = dict("LATEST")(1)将抛出一个异常。所以你得先检查一下。现在您有了dict字典,其中dict.Keys是name-s,dict.Values是每个键的version-s和description-s。
如果您只想创建一个List(Of String)
Dim patt = "(.*?.)(\(\d+.\d+.\d+\)|\d+.\d+.\d+)(=?.*?-.)?(.*)"
Dim matches = Regex.Matches(txtIn.Text, patt)
Dim lst As New List(Of String)
For Each m As Match In matches
lst.Add(m.Groups(1).Value.Trim({" "c, ":"c}))
lst.Add(m.Groups(2).Value.Trim({" "c, ":"c}))
If Not String.IsNullOrWhiteSpace(m.Groups(4).Value) Then
lst.Add(m.Groups(4).Value.Trim({" "c, ":"c}))
End If
Next下面是前面的regex101模式的RegEx测试。
编辑:建议的改进
根据你上一次的编辑,澄清了你想要达到的目标,我想建议如下:
Dictionary(Of String, List(Of String)),直接使用ListView控件的ListViewItemCollection。这样,只需要一个ForEach循环就可以完成这项工作。Dim patt = "(.*?.)(\(\d+.\d+.\d+\)|\d+.\d+.\d+)(=?.*?-.)?(.*)"
Dim matches = Regex.Matches(sOutput.Text, patt)
lvPackages.BeginUpdate()
lvPackages.Items.Clear()
For Each m As Match In matches
Dim key = m.Groups(1).Value.Trim({" "c, ":"c})
If key.IndexOf("latest", StringComparison.InvariantCultureIgnoreCase) >= 0 Then
Continue For
End If
Dim lvi As ListViewItem
If key.IndexOf("installed", StringComparison.InvariantCultureIgnoreCase) >= 0 Then
lvi = lvPackages.Items.OfType(Of ListViewItem).LastOrDefault
'Just in case
If lvi IsNot Nothing Then
lvi.SubItems.Add(m.Groups(2).Value.Trim({" "c, ":"c}))
End If
Continue For
End If
lvi = New ListViewItem(key)
'Add the version..
lvi.SubItems.Add(Regex.Match(m.Groups(2).Value, "\d+.\d+.\d+").Value)
If Not String.IsNullOrWhiteSpace(m.Groups(4).Value) Then
'Add the description..
lvi.SubItems.Add(m.Groups(4).Value.Trim({" "c, ":"c}))
End If
lvPackages.Items.Add(lvi)
Next
'Optional: Auto size each column to fit the longest item.
lvPackages.Columns.Cast(Of ColumnHeader).
ToList().ForEach(Sub(x) x.Width = -1)
lvPackages.EndUpdate()看看这个。
发布于 2020-02-23 13:44:27
这是正则表达式的工作。您可以使用可变数量的空格字符来解析字符串。对于vb,我不确定,但是在python中,我会这样做(但是通过基于\r的拆分来迭代每一行):

https://stackoverflow.com/questions/60362707
复制相似问题