title: Compare Text Files as Template
body: <p>I have certain files that are uploaded to my website, and I would like to compare them to known templates of files for a potential match.</p>
  
  <p>For example-sake, I could have a text file with the following "template".</p>
  
  <p><em>template.txt</em></p>
  
  <pre><code>Hi, my name is (\w+), and I like (\w+).
  </code></pre>
  
  <p>Then, say someone uploads the following file.</p>
  
  <p><em>bio.txt</em></p>
  
  <pre><code>Hi, my name is James, and I like cats.
  </code></pre>
  
  <p>This would match the template, and thus trigger a rule associated with that match.</p>
  
  <p>Obviously, this is a <em>very</em> watered down example, but should get the point across. The real regex would be more expressive to match something specific like any URL, Bitcoin address, etc. (I already have expressions for all of that).</p>
  
  <p>I have tried regular expressions, as this is how I do other, more complicated matching with uploaded files, but this really becomes cumbersome when it comes to challenges such as new lines and escaping tons of characters. For example, I've had 20-line+ files as the template - converting all the newlines to <code>\n</code>, and escaping all parenthesis, brackets, etc. is crazy, not to mention this feels super overkill? Regular expressions don't seem to like using multi-lines for the pattern part from what I can tell.</p>
  
  <p>Is there a better way to match this "template" file against the provided file? Any searches I do for "comparing text files" gives me more-so line-by-line comparisons, such as for showing differences in lines of code. I need to exactly match with the patterns expected; if the template does not match, then the overall result should be no match of course.</p>
  
  <p>I currently do have something like this setup with the regular expressions, and I basically just throw them both through <code>preg_match()</code>, but is there a better, more statistical way I should be using for this problem?</p>
  
  <p>My website is programmed in PHP, so I would need to implement it as such. Afraid I don't get access to much on the shell of a shared (cheap) host.</p>