我正在尝试从Web文件中解析一些字符串,并将解析后的内容组织到listview中。嗯,这并不像我想的那么容易哈哈。
这是我正在尝试解析的字符串
array(7) { [0]=> string(1) "0" [1]=> string(5) "hefqn" [2]=> string(23) "SP-Branding SP-Digital " [3]=> string(18) "all hail the hefqn" [4]=> string(11) "rune-server" [5]=> string(6) "tickle" [6]=> string(23) "http://www.prntscr.com" }
array(7) { [0]=> string(1) "1" [1]=> string(6) "hefqn2" [2]=> string(23) "SP-Branding SP-Digital " [3]=> string(18) "2ll hail the hefqn" [4]=> string(11) "rune-server" [5]=> string(6) "tickle" [6]=> string(23) "http://www.prntscr.com" }
这是数据库中的两个条目,第一个是第一个条目,第二个是第二个条目。
现在上面代码中我正试图解决的文本就是这个(我将把 * 放在我试图解析的文本之前和之后)
array(7) { [0]=> string(1) "***0***" [1]=> string(5) "***hefqn***" [2]=> string(23) "***Digital***" [3]=> string(18) "***test***" [4]=> string(11) "***rune-server***" [5]=> string(6) "***tickle***" [6]=> string(23) "***http://www.prntscr.com***" }
array(7) { [0]=> string(1) "***1***" [1]=> string(6) "***hefqn2***" [2]=> string(23) "***Branding***" [3]=> string(18) "***test2***" [4]=> string(11) "***rune-server***" [5]=> string(6) "***tickle***" [6]=> string(23) "***http://www.prntscr.com***" }
现在在解析文本时,它在foreach语句中,因为我抓取了多个数据库条目,这些条目都具有该布局。
listview看起来像这样 http://i.stack.imgur.com/rqCXA.png
我一直在努力解决这个问题一小时了,我似乎无法做到这一点。
有没有人对如何做到这一点有任何想法?非常感谢!!
PS这是我尝试过的,它会添加到每个子项,而不是每行一次
Dim test As New WebClient
Dim s As String = test.DownloadString(TheStringIPosted)
For Each myMatch1 As Match In New Regex("""(.*?)""", RegexOptions.None).Matches(s)
Dim lvi As ListViewItem
lvi = ListView1.Items.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
lvi.SubItems.Add(myMatch1.Groups.Item(1).ToString)
Next
答案 0 :(得分:0)
我将它编辑为与您已经在做的事情相匹配
我会做这样的事情(未经测试,但应该让你去):
Dim regMatch As New Regex("""(.*?)""")
For Each item as String in (the collection of strings you want to look through)
Dim regMatches As MatchCollection = regMatch.Matches(item)
Dim lvi As New ListViewItem()
For x = 0 to 6
lvi.SubItems.Add(regmatches(x).Value)
Next
lvi = ListView1.Items.Add(lvi)
Next
使用.matches会很有用,因为它实际上会将信息保存到数组中,因为你有7个匹配的东西,只需使用for语句(0到6)来抓取这些东西并将它们放入适当的细胞。再一次,语法并不完美,但你应该明白这一点。
答案 1 :(得分:0)
另一个选择是使用正则表达式查找五个组:
Dim test As New WebClient
Dim s As String = test.DownloadString(TheStringIPosted)
For Each myMatch1 As Match In New Regex("""(.+?)"".+""(.+?)"".+""(.+?)"".+""(.+?)"".+""(.+?)"".+", RegexOptions.None).Matches(s)
Dim lvi As New ListViewItem(myMatch1.Groups(1).Value)
lvi.SubItems.Add(myMatch1.Groups(2).Value)
lvi.SubItems.Add(myMatch1.Groups(3).Value)
lvi.SubItems.Add(myMatch1.Groups(4).Value)
lvi.SubItems.Add(myMatch1.Groups(5).Value)
lvi = ListView1.Items.Add(lvi)
Next