title: Extract GUID from line in C#
body: <p>I try to simplify some legacy code using IndexOf to retrieve a GUID from lines. Can I further simplify the code below to get rid of using guids.Any and guids.First?</p>
  
  <pre><code>// Code using regular expression
  private static string RetrieveGUID2(string[] lines)
  {
      string guid = null;
      foreach (var line in lines)
      {
          var guids = Regex.Matches(line, @"[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?")
              .Cast&lt;Match&gt;().Select(m =&gt; m.Value);
          if (guids.Any())
          {
              guid = guids.First();
              break;
          }
      }
      return guid;
  }
  </code></pre>
  
  <p>Below the legacy code given in a sample that compiles:</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Text.RegularExpressions;
  
  namespace ConsoleApplication2
  {
      internal class Program
      {
          private static void Main(string[] args)
          {
              var lines = new[]
              {
                  "&lt;/ItemGroup&gt;\n",
                  "&lt;PropertyGroup\n",
                  "Label = \"Globals\"&gt;\n",
                  "&lt;ProjectGuid&gt;{A68615F1-E672-4B3F-B5E3-607D9C18D1AB}&lt;/ProjectGuid&gt;\n",
                  "&lt;/PropertyGroup&gt;\n"
              };
  
              Console.WriteLine(RetrieveGUID(lines));
              Console.WriteLine(RetrieveGUID2(lines));
          }
  
          // Legacy code
          private static string RetrieveGUID(string[] lines)
          {
              string guid = null;
              foreach (var line in lines)
              {
                  var startIdx = line.IndexOf("&lt;ProjectGuid&gt;{", StringComparison.Ordinal);
                  if (startIdx &lt; 0) continue;
                  var endIdx = line.IndexOf("&lt;/ProjectGuid&gt;", StringComparison.Ordinal);
                  if (endIdx &lt; 0) continue;
                  startIdx += "&lt;ProjectGuid&gt;".Length;
                  var guidLen = endIdx - startIdx;
                  guid = line.Substring(startIdx, guidLen);
                  break;
              }
              return guid;
          }
  
          // Code using regular expression
          private static string RetrieveGUID2(string[] lines)
          {
              string guid = null;
              foreach (var line in lines)
              {
                  var guids = Regex.Matches(line, @"[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?")
                      .Cast&lt;Match&gt;().Select(m =&gt; m.Value);
                  if (guids.Any())
                  {
                      guid = guids.First();
                      break;
                  }
              }
              return guid;
          }
      }
  }
  </code></pre>