title: Regex for matching token wrapped in %
body: <p>I have user-entered text with potentially mistyped "tokens" I'm trying to find <strong>using PHP</strong>.</p>
  
  <p>A valid "token" is any number of word characters wrapped in percent signs - so <code>%blah%</code> <code>%blah_moreblah%</code>.  Basically I'm looking for tokens where the user may have forgotten to put a leading or trailing '%'.  I'm also looking for tokens in the valid format - as at this point in my code, all replaceable tokens have already been replaced.</p>
  
  <p>So, the 3 situations I'm looking for are (to borrow regex syntax): <code>%\w+</code>, <code>%\w+%</code>, or <code>\w+%</code>.</p>
  
  <p>In English, what I'm looking for is, "a string that starts with a % and/or ends with a % and contains only word characters'</p>
  
  <p>The regex I have this far is: <code>(%*\w+%*)</code>, but you'll notice it matches every single word.  I'm stuck on making a match <em>require</em> at least a leading or a trailing %.</p>
  
  <p><strong>Edit</strong>: Initially I tried to have all 3 situations found with their own regex.  However, I was finding that the regex for finding tokens in the first situation would also find tokens in the second situation, just without the trailing %.  For example, <code>/(%\w+)/</code>, when checked against <code>%before %both%</code>, would match <code>%before</code> and <code>%both</code>.</p>