Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression

Status
Not open for further replies.

byleth

Programmer
Feb 27, 2004
70
0
0
PT
Hi,

I need a regular expression that matches this string:


[{x:y:z}][{k:l:m}] ... with this pattern repeating n times


i want to capture each word, that is: get the 'x', 'y', 'z', 'k'...

i've tried something like this

Dim re As New Regex("[{(\w):(\w):(\w)}]")
Dim match As Match = re.Match(str)
MessageBox.Show(match.Value)

but the match.value is '{'

i really don't know anything about regexp and would apreciate a sugestion,


tx
 
The closest I can get is:

(?<=\[{)(\w:){2}\w(?=\}])

and then use string.split on the :

for example

Code:
    Dim s As String = ""
    Dim rx As New Regex("(?<=\[{)(\w:){2}\w(?=\}])")
    For Each m As Match In rx.Matches("[{x:y:z}][{k:l:m}]")
      s += m.ToString + ":"
    Next
    Dim s1() As String
    s1 = s.Split(":".ToCharArray)
    's1.Length and s1.Length - 1 will be empty strings
    ListBox1.Items.Clear()
    For a As Integer = 0 To s1.Length - 2
      ListBox1.Items.Add(s1(a).ToString)
    Next
 
Two things.

1) The comment in my previous post should have read:
's1.Length - 1 will be empty an string

(S1.Length is obviously out of range - sorry about that)

2) A solution:

(?<=\[{)(\w)(?=:\w:\w}])|(?<=\[{\w:)(\w)(?=:\w}])|(?<=\[{\w:\w:)(\w)(?=}])

as in :

Code:
    ListBox1.Items.Clear()
    Dim rx1 As New Regex("(?<=\[{)(\w)(?=:\w:\w}])|(?<=\[{\w:)(\w)(?=:\w}])|(?<=\[{\w:\w:)(\w)(?=}])")
    For Each m1 As Match In rx1.Matches("[{x:y:z}][{k:l:m}]")
      ListBox1.Items.Add(m1.ToString)
    Next

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top