user4951
user4951

Reputation: 33050

How to use regular expression to get strings between 2 strings

Say the string is something like

bla bla bla bla (cat) bladfdskdfd dsgdsdksf (dog) dfdshfdskdskfsdfkhsdf sdkfhdsfkdf (kathy) fdsfhdskfhdsfkd (doggy)

I want a generic.list (of string) containing

cat

dog

kathy

doggy

How to do that with regular expression in vb.net

Later I want to do something more complicated like getting all strings between "url":" and ", from this strings

google.search.WebSearch.RawCompletion('1', {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"https://eproc.pu.go.id/publik/eproc2011/semieprocplus/info_lelangprogress.asp?tid\u003d12\u0026id\u003d%7BC0B5ED00-F369-4700-B93A-B0677B63D9EA%7D\u0026u\u003d7\u0026t\u003d3\u0026d\u003d1","url":"https://eproc.pu.go.id/publik/eproc2011/semieprocplus/info_lelangprogress.asp%3Ftid%3D12%26id%3D%257BC0B5ED00-F369-4700-B93A-B0677B63D9EA%257D%26u%3D7%26t%3D3%26d%3D1","visibleUrl":"eproc.pu.go.id","cacheUrl":"","title":"Informasi Proyek","titleNoFormatting":"Informasi Proyek","content":"Jl. batu \u003cb\u003eKucing\u003c/b\u003e Gg. Tuah No.4 Tanjungpinang. NPWP : 152742110214000. No. Agency, : -. Tgl. Agency, : -. Nilai, : 90.76. Waktu, : 270 Hari. Nilai kontrak, : Rp."},{"GsearchResultClass":"GwebSearch","unescapedUrl":"https://eproc.pu.go.id/publik/dinaspu/kegiatan/info_paket.asp?id\u003d%7B4BD47F74-233B-4D49-BB60-4229023668C6%7D","url":"https://eproc.pu.go.id/publik/dinaspu/kegiatan/info_paket.asp%3Fid%3D%257B4BD47F74-233B-4D49-BB60-4229023668C6%257D","visibleUrl":"eproc.pu.go.id","cacheUrl":"","title":"Informasi Proyek","titleNoFormatting":"Informasi Proyek","content":"9 Apr 2010 \u003cb\u003e...\u003c/b\u003e Sub Kegiatan, : PEMBANGUNAN JALAN. Paket, : Peningkatan jalan s.d hotmix Jl . \u003cb\u003eKucing\u003c/b\u003e Kel.Purwosari. Rupiah Murni, : 1449000000 \u003cb\u003e...\u003c/b\u003e"}],"cursor":{"resultCount":"10","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3}],"estimatedResultCount":"10","currentPageIndex":2,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d12\u0026hl\u003den\u0026q\u003d+kucing+site:eproc.pu.go.id","searchResultTime":"0.05"}}, 200, null, 200)

Upvotes: 1

Views: 692

Answers (2)

Jamie Carsten
Jamie Carsten

Reputation: 51

So in searching for a similar problem I came across this post. My task was to parse the full path of a tree node (which is represented as say "root\child\grandchild\greatgrandchild...\currentNode" to pick a level of ancestor to use. While irfanmcsd's answer was elegant, I'm having difficulty geting my arms around setting up the pattern correctly for slashes (and would really like to get better at regular expressions.)

Here was my solution for my particular problem:

    Dim exp As New Regex("\\", RegexOptions.IgnoreCase)
    Dim ancestors() As String

    ancestors = exp.Split(calledFromTreeNode.FullPath)

I now have an array of values to accomplish what I need.

Granted it's not the same problem, but it irfanmcsd's solution helped drive my own. Now I just need get a better understanding of patterns!

Upvotes: 1

irfanmcsd
irfanmcsd

Reputation: 6561

If you want to extract values from string and generate generic list in vb.net you can try it

Private Function Fetch_Items(Text As String) As List(Of Generic_List)
Dim pattern As String = "\((?<value>(.)*?\))"
Dim _lst As New List(Of Generic_List)()
Dim VDMatch As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Text, pattern)
While VDMatch.Success
    _lst.Add(VDMatch.Groups("value").Value)
    VDMatch = VDMatch.NextMatch()
End While

Return _lst
End Function

This function will extract all strings containing within ( ) and generate generic list.

Upvotes: 1

Related Questions