title: Lua: String.match vs String.gmatch?
body: <p>I have both the "5.1 Reference Manual" and the "Programming in Lua: 3rd Ed." in front of me. Reading these, as well as numerous searches on the web, still leave me a bit confused when it comes to using <code>string.match</code> and <code>string.gmatch</code>.</p>
  
  <p>I understand that they both are used to locate patterns.  </p>
  
  <p>Here are an example they use in the "Reference Manual" for string.gmatch:</p>
  
  <pre><code>s = "hello world from Lua"
  for w in string.gmatch (s, "%a+") do
      print(w)
  end
  </code></pre>
  
  <p>I understand that this will iterate over all of the words in <code>s</code> and print them each one per line.</p>
  
  <p>Here is an example they use in the "Programming in Lua" book for <code>string.match</code>:</p>
  
  <pre><code>date = "Today is 17/7/1990"
  d = string.match(date, "%d+/%d+/%d+")
  print(d) -- prints 17/7/1990
  </code></pre>
  
  <p>What I'm confused about is when is it appropriate to use one over the other?</p>
  
  <p>For example, you had code you wanted to parse that had contained the same pattern dozens of times throughout it.  This pattern contained variables you needed, which would be a better choice? Example code below (x's are all variable data that differs from the other lines.  data can be any garbage you didn't care about looking for and it was all just noise):</p>
  
  <pre><code>Header contains variable (HERE) and (HERE) I want.  
      data data data data data data data data 
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      data data data data data data data
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      data data data data data data data data data 
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
      data data data data data data data data data data data data data data data data data data data data data data data data 
      &lt;Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX&gt;
  Footer here also has three variables I want (here)/(here) and (here)
  </code></pre>
  
  <p>This code obviously has a pattern to it.  But, if wanted to create a simple function that parsed the data and grabbed the variables, which is the better choice?  </p>
  
  <pre><code>function match(data)
      local f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt = data:match("&lt;Font Typeset:(.-) Font Color:(.-) Font Xpos:(.-) Font Ypos:(.-) Font Bold:(.-) Font Uline:(.-) Font Italic:(.-) Font Text:(.-)&gt;
      print(f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt)
  end
  </code></pre>
  
  <p>...or...</p>
  
  <pre><code>function gmatch(data)
      local f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt = data:gmatch("&lt;Font Typeset:(.-) Font Color:(.-) Font Xpos:(.-) Font Ypos:(.-) Font Bold:(.-) Font Uline:(.-) Font Italic:(.-) Font Text:(.-)&gt;
      print(f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt)
  end
  </code></pre>
  
  <ol>
  <li><p>Does gmatch just iterate over the entire code (data in this example) and return all instances where the pattern is true where match only does the first?</p></li>
  <li><p>In what scenarios is one better than the other?</p></li>
  </ol>
  
  <p>ETA: I added a header and footer to the example code.  This header and footer both contain variables I want to use.  Now this entire chunk of code (header/body/footer) are repeated numerous times throughout the same input file that I want to parse.  So there are patterns within patterns.  </p>