Pattern: GUID

“GUID” is one of the patterns that you can select on the Match panel. Use it to make a field match a globally unique identifier such as {7E9081B5-9A6D-4CC1-A8C3-47F69FB4198D}.

This example shows how you can use the “GUID” pattern to validate some text as being a proper GUID formatted with braces and hyphens.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample that has a block of valid and a block of invalid GUIDs:
    {D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D}
    {594ABD08-C02C-4827-8ABF-A128006848A3}
    {B59511BD6A5F4DF09ECF562A108D8A2E}
    69593D62-71EA-4548-85E4-04FC71357423
    677E2553DD4D43B09DA77414DB1EB8EA
    
    {99367d71-08ad-4aec-8e73-55ae151614f9}
    {5ba3bba3-729a-4717-88c1-b7c4b7ba80db}
    {7e9081b59a6d4cc1a8c347f69fb4198d}
    0c74f13f-fa83-4c48-9b33-68921dd72463
    b4b2fb69c6244e5eb0698e0c6ec66618
    
    DE011585-7317-4C0D-A9AF-0AF2C5E61DB8}
    {E8038A99-A409-490B-B11A-159A3496426D
    {D44EF4F4-280B47E5-91C7-261222A59621}
    {5EDEB36C-9006-467A8D04-AFB6F62CD7D2}
    {D995AC86-5697-4683-A34F2B778BD837ED}
    {283B67B2-430F-4E6F-97E6-19041992-C1B0}
    2F2B15A5-2615-4748-BDABA124210F15EC
    07D267AB-7126-45EB8FFC-51994D05F0B2
  3. Select the text you just pasted in, and click the Unmark button on the Samples panel. This removes all fields from the Match panel that may be left over from a previous regular expression.
  4. Set the subject scope to “line by line”.
  5. Select the first GUID {D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D} in the sample text.
  6. Click the Mark button. RegexMagic automatically adds a field using the GUID pattern. It automatically detects the GUID has braces, hyphens, and is uppercase and sets those options.
  7. In the block with lowercase GUIDs in the sample, select the first GUID {99367d71-08ad-4aec-8e73-55ae151614f9}.
  8. Click the 1 button. It’s important to use this button instead of the Mark button, because we want to provide an additional sample for the same field instead of adding a new field. RegexMagic automatically determines that the GUID pattern is still the proper pattern for field 1. It also determines that both GUIDs have braces and hyphens, and that one is uppercase and the other lowercase. RegexMagic changes the pattern to be case insensitive.
  9. On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”.
  10. Set the “field validation mode” to “strict”.
  11. On the Regex panel, select “C# (.NET 2.0–8.0)” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \A\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}\z

    Required options: Case insensitive.
    Unused options: Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.

  12. The Samples panel now highlights all the valid GUIDs matched by our regular expression:
    {D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D}
    {594ABD08-C02C-4827-8ABF-A128006848A3}
    {B59511BD6A5F4DF09ECF562A108D8A2E}
    69593D62-71EA-4548-85E4-04FC71357423
    677E2553DD4D43B09DA77414DB1EB8EA
    
    {99367d71-08ad-4aec-8e73-55ae151614f9}
    {5ba3bba3-729a-4717-88c1-b7c4b7ba80db}
    {7e9081b59a6d4cc1a8c347f69fb4198d}
    0c74f13f-fa83-4c48-9b33-68921dd72463
    b4b2fb69c6244e5eb0698e0c6ec66618
    
    DE011585-7317-4C0D-A9AF-0AF2C5E61DB8}
    {E8038A99-A409-490B-B11A-159A3496426D
    {D44EF4F4-280B47E5-91C7-261222A59621}
    {5EDEB36C-9006-467A8D04-AFB6F62CD7D2}
    {D995AC86-5697-4683-A34F2B778BD837ED}
    {283B67B2-430F-4E6F-97E6-19041992-C1B0}
    2F2B15A5-2615-4748-BDABA124210F15EC
    07D267AB-7126-45EB8FFC-51994D05F0B2

Matching Specific GUIDs

The GUID pattern can only be used to match arbitrary GUIDs. If you want to match specific GUIDs, use the “literal text” pattern to match a single GUID, or the “list of literal text” pattern to match one from a list of GUIDs.

Continuing from the above example, to get a regex that matches only those two GUIDs that we marked, select “list of literal text” in the “pattern to match field” drop-down list. RegexMagic automatically adds the two GUIDs to the pattern. Turn on the “case insensitive” checkbox to make the regex case insensitive.

Reference