Results 1 to 3 of 3

Thread: [RESOLVED] How to Extract GUIDs out of a string?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Resolved [RESOLVED] How to Extract GUIDs out of a string?

    I am in Vb.NET 2008.

    I want to extract or split all the GUIDS out of a string like this:

    "331|-1|2||True|0|False|6f85d48a-9219-4a9a-98bf-e6fb2319727c||False/-1|0|3||True|0|False|5f3d89d2-461c-47f8-ace7-4f8e2e37879f||False|"

    Here is the code I have and it does not work.

    Dim s() As String = Regex.Split("331|-1|2||True|0|False|6f85d48a-9219-4a9a-98bf-e6fb2319727c||False/-1|0|3||True|0|False|5f3d89d2-461c-47f8-ace7-4f8e2e37879f||False|".ToUpper , "^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$")

    Output desired:

    6f85d48a-9219-4a9a-98bf-e6fb2319727c

    5f3d89d2-461c-47f8-ace7-4f8e2e37879f

  2. #2
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: How to Extract GUIDs out of a string?

    Would you like them as a string or GUID? You can use the String.Split ("|") to split them up. Trim it first.

    Then compare the length i guess. Or see if the string contains a "-"

    If you want to convert it to a GUID then you can
    Dim g As String
    Dim instance As New Guid(g)

    and it will throw an exception if not in the right format.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: How to Extract GUIDs out of a string?

    Actually I just got it!

    Dim ms As MatchCollection = Regex.Matches(_TreeNode.Parent.ValuePath.ToUpper, "[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}")
    Me.ParentGUID = ms.Item(ms.Count - 1).Value.ToLower '--Get the last item, it is the parent guid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width