Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find matching guid in string [duplicate]

Tags:

c#

regex

I need to find the matching GUID in string using Regex

string findGuid="hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f"
var guid = Regex.Match(m.HtmlBody.TextData, @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$").Value;
like image 339
user1551433 Avatar asked Nov 02 '12 06:11

user1551433


1 Answers

You may use Guid.TryParse on splitted array. Something like:

string findGuid = "hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f";
string[] splitArray = findGuid.Split();
List<Guid> listofGuid = new List<Guid>();
foreach (string str in splitArray)
{
    Guid temp;
    if (Guid.TryParse(str, out temp))
        listofGuid.Add(temp);
}

This will give you two items in the list of Guid

EDIT: For the new string as per comment of the OP,

string findGuid="hi sdkfj x-Guid:1481de3f-281e-9902-f98b-31e9e422431f sdfsf x-Guid:1481de3f-281e-9902-f98b-31e9e422431f"

Multiple split delimiters may be specified something like:

string[] splitArray = findGuid.Split(' ', ':');
like image 168
Habib Avatar answered Sep 18 '22 20:09

Habib