[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
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.
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.