2014-01-26 20 views
0

我試圖解析網絡文件中的一些字符串,並將解析的內容組織到列表視圖中。那麼它不像我想的那麼容易哈哈。解析foreach語句中的數據,然後添加到列表視圖

這是我試圖解析

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語句中,因爲我抓取了多個數據庫條目,它們都具有該佈局。

列表視圖看起來像這樣 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

我編輯它那種匹配你已經做

我會做一些像什麼這種(未經測試,但應該讓你去):

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因爲它會esentially信息保存到一個數組將是有益的,因爲你有7個東西,你是匹配的,只需使用for語句(0到6)來獲取這些內容並將它們放入適當的單元格中。再次,語法不完美,但你應該明白。

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 
相關問題