We help IT Professionals succeed at work.

Regex - Asp.net

I want to get
SN1 & date of SN1
GM2 & date of GM2
....

i mean i want to get only snapshot names and dates

how should i do this with regex ?

 
|-ROOT
--Snapshot Name        : SN1
--Snapshot Desciption  :
--Snapshot Created On  : 12/4/2011 3:10:9
--Snapshot State       : powered off
--|-CHILD
----Snapshot Name        : GM2
----Snapshot Desciption  :
----Snapshot Created On  : 12/4/2011 3:16:48
----Snapshot State       : powered off
----|-CHILD
------Snapshot Name        : DeepBackup
------Snapshot Desciption  : powered_on_state
------Snapshot Created On  : 12/4/2011 3:18:49
------Snapshot State       : powered off

Open in new window

Comment
Watch Question

IT Guru
Most Valuable Expert 2011
Commented:
Something like this I believe:

    Dim sourcestring as String = "replace with your source string"
    Dim re As Regex = New Regex("--Snapshot Name\s*:(.*)[\s\S]*?--Snapshot Created On\s*:(.*)")
    Dim mc as MatchCollection = re.Matches(sourcestring)
    Dim mIdx as Integer = 0
    For each m as Match in mc
      For groupIdx As Integer = 0 To m.Groups.Count - 1
        Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
      Next
      mIdx=mIdx+1
    Next

(code generated by myregextester.com)

Author

Commented:
Wow not exactly worked the result :

"Snapshot name":"--Snapshot Name : SN1\r\n--Snapshot Desciption : \r\n--Snapshot Created On : 12/4/2011 3:10:9\r"

"Snapshot date":"0"


"Snapshot name":"--Snapshot Name : GM2\r\n----Snapshot Desciption : \r\n----Snapshot Created On : 12/4/2011 3:16:48\r"

"Snapshot date":"0"
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
Maybe use the multiline option?
    Dim re As Regex = New Regex("--Snapshot Name\s*:(.*)[\s\S]*?--Snapshot Created On\s*:(.*)",RegexOptions.Multiline)

Author

Commented:
Actually for this :


Get Snapshot:
|-ROOT
--Snapshot Name        : yedek
--Snapshot Desciption  :
--Snapshot Created On  : 12/4/2011 3:10:9
--Snapshot State       : powered off
--|-CHILD
----Snapshot Name        : yedek2
----Snapshot Desciption  :
----Snapshot Created On  : 12/4/2011 3:16:48
----Snapshot State       : powered off
----|-CHILD
------Snapshot Name        : yedek3
------Snapshot Desciption  : powered_on_state
------Snapshot Created On  : 12/4/2011 3:18:49
------Snapshot State       : powered off
------|-CHILD
--------Snapshot Name        : yedek3
--------Snapshot Desciption  : powered_on_state
--------Snapshot Created On  : 12/4/2011 3:27:28
--------Snapshot State       : powered off

Open in new window



it gets the following result :
i am converting it to json sorry

[{"ad":"--Snapshot Name : yedek\r\n--Snapshot Desciption : \r\n--Snapshot Created On : 12/4/2011 3:10:9\r","tarih":"0"},{"ad":" yedek\r","tarih":"1"},{"ad":" 12/4/2011 3:10:9\r","tarih":"2"},{"ad":"--Snapshot Name : yedek2\r\n----Snapshot Desciption : \r\n----Snapshot Created On : 12/4/2011 3:16:48\r","tarih":"0"},{"ad":" yedek2\r","tarih":"1"},{"ad":" 12/4/2011 3:16:48\r","tarih":"2"},{"ad":"--Snapshot Name : yedek3\r\n------Snapshot Desciption : powered_on_state\r\n------Snapshot Created On : 12/4/2011 3:18:49\r","tarih":"0"},{"ad":" yedek3\r","tarih":"1"},{"ad":" 12/4/2011 3:18:49\r","tarih":"2"},{"ad":"--Snapshot Name : yedek3\r\n--------Snapshot Desciption : powered_on_state\r\n--------Snapshot Created On : 12/4/2011 3:27:28\r","tarih":"0"},{"ad":" yedek3\r","tarih":"1"},{"ad":" 12/4/2011 3:27:28\r","tarih":"2"}]

Open in new window

Author

Commented:
A little  code change and it worked thqanks