title: SpamAssassin Regular Expression not matching
body: <p>I have read similar questions here, but being that all regular expressions are not created equal, I was not able to find a solution to my problem.</p>
  
  <p>I am working on a rule for SpamAssassin that will tell if the recipient's e-mail username is contained in the body of the message. For example, an e-mail sent to <code>testuser@somedomain.com</code> contains <code>testuser</code> in the body of the message. I have written and tested a regular expression on <a href="https://regex101.com/" rel="nofollow">Regex-101</a> and am able to match it as expected, but when I create the rule it does not work when I test it in SpamAssassin.</p>
  
  <p>Here is the expression:</p>
  
  <pre><code>/To:\s([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  </code></pre>
  
  <p>What is <strong>should</strong> do is match an e-mail address in the <code>To:</code> header (or anywhere in the body of the message matching the format <code>To: user@somedomain.com</code>. As I mentioned before, the expression matches as expected on Regex-101, but when I make a rule in SpamAssassin, it does not match.</p>
  
  <p>If I remove the leading <code>To:\s</code> then it does match, but I am only concerned with matching the e-mail in the <code>To:</code> header.  I have tried these various mutations of the expression:</p>
  
  <pre><code>/To:\s([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  /To: ([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  /To:[\s]{0,2}([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  /:\s([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  
  /\s([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  </code></pre>
  
  <p>None of the previous rules match, but this one does:</p>
  
  <pre><code>/([a-z0-9][-a-z0-9]{1,19})\@somedomain\.com[a-z0-9\s=;:\/\.-]*\1\b/i
  </code></pre>
  
  <p>Here is the text I am using for testing:</p>
  
  <pre><code>Subject: Test spam mail (GTUBE) private jet rental
  Message-ID: &lt;GTUBE1.1010101@example.net&gt;
  Date: Wed, 23 Jul 2003 23:30:00 +0200
  From: Sender &lt;sender@live.com&gt;
  To: recipient@somedomain.com
  Precedence: junk
  MIME-Version: 1.0
  Content-Type: text/plain; charset=us-ascii
  Content-Transfer-Encoding: 7bit
  recipient
  This is the GTUBE, the
      Generic
      Test for
      Unsolicited
      Bulk
      Email
  </code></pre>
  
  <p>Which <em>should</em> match on the <code>To: recipient@somedomain.com</code> .... <code>recipient</code>, but I can only get it to match when I remove the <code>To:\s</code> from the expression. The full expression tests out in Regex-101, so it seems to be something specific to SpamAssassin, but I'm not sure.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is an updated version of the expression to NOT allow a dash at the end of the username, but will allow in the middle:</p>
  
  <pre><code>/\bTo:\s([a-z0-9][-a-z0-9]{0,18}[a-z0-9])\@somedomain\.com[a-z0-9\s=;:\/\.-]*\b\1\b/i
  </code></pre>