9

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;
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
user1551433
  • 115
  • 1
  • 1
  • 7
  • 1
    Doesn't your code work? Which is the problem? – Marco Nov 02 '12 at 06:34
  • i am not able to get matching guids – user1551433 Nov 02 '12 at 06:34
  • http://stackoverflow.com/questions/856327/regex-to-get-a-guid-from-a-email-reply – Mike Trusov Nov 02 '12 at 06:35
  • 1
    I disagree this is not a duplicate – gorhal Nov 24 '16 at 08:03
  • This is actually not a 100% duplicate, however you can certainly use the top answer for the question that is marked as duplicate and adjust it to remove the ^ character which indicates that the expression must match from the start of the string and the $ character which indicates that the expression must match from the end of the string. You could then add (?i) to the start to signal case insensitive so that you do not need to use RegexOptions.IgnoreCase, then you end up with: (?i)[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]? – Darren Sep 13 '18 at 00:58

5 Answers5

25

If you would like to get the GUID using a Regex pattern. Then, try this pattern

(\{){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}

Example

string findGuid = "hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f"; //Initialize a new string value
MatchCollection guids = Regex.Matches(findGuid, @"(\{){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}"); //Match all substrings in findGuid
for (int i = 0; i < guids.Count; i++)
{
    string Match = guids[i].Value; //Set Match to the value from the match
    MessageBox.Show(Match); //Show the value in a messagebox (Not required)
}

Regex Matcher matching TWO GUIDs from a string

Notice: I've used the same pattern you've provided but simply removed the ^ character which indicates that the expression must match from the start of the string. Then, removed the $ character which indicates that the expression must match from the end of the string.

More information about Regular Expressions can be found here:
Regular Expressions - a Simple User Guide and Tutorial

Thanks,
I hope you find this helpful :)

Community
  • 1
  • 1
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
9

Looks like You use incorrect regular expression. If You need guid

{8}-{4}-{4}-{4}-{12}

should be like

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

You may try in this way:

string findGuid="hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f";
    string regexp = @"[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}";
    if (Regex.IsMatch(findGuid, regexp))
    {
        Console.WriteLine(
        Regex.Match(findGuid, regexp).Value
        );

    }
Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
2

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(' ', ':');
Habib
  • 219,104
  • 29
  • 407
  • 436
  • What does Split() with no parameter do on a string? – nawfal Nov 02 '12 at 06:39
  • 2
    @nawfal, split it on a space, just learned it [couple of days ago on SO](http://stackoverflow.com/a/13139223/961113) – Habib Nov 02 '12 at 06:40
  • 1
    if string is like this string findGuid="hi sdkfj x-Guid:1481de3f-281e-9902-f98b-31e9e422431f sdfsf x-Guid:1481de3f-281e-9902-f98b-31e9e422431f" – user1551433 Nov 02 '12 at 06:40
  • @user1551433, In that particular case, you have to split on multiple characters: `string[] splitArray = findGuid.Split(' ', ':');` – Habib Nov 02 '12 at 06:43
2
Pattern = "\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}-\d+"
string input = "name=4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607.jpg This is input string";
    Match m = Regex.Match(input, @"\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}", RegexOptions.IgnoreCase);
    string output = null;
    if (m.Success)
        output = m.Value;
Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43
-1

With .NET 4.0 and higher you can use Guid.TryParse. Here is an extension method that I use.

public static bool IsGuid(this string checkGuid)
{
    if (string.IsNullOrEmpty(checkGuid)) return false;
    Guid resultGuid;
    return Guid.TryParse(checkGuid, out resultGuid);
}

And here is how you can call that.

public MyMethod(string myToken)
{
    if (myToken.IsGuid())
    {
        // Do something
    }
}
Ken Palmer
  • 2,355
  • 5
  • 37
  • 57