title: Regex to match full name with comma or space
body: <p>I'm working with CommonMark and I've created a '@mention' parser that will return a link when an @mention is found. However, I'm tweaking it to link full names instead of usernames. Here's what I currently have:</p>
  
  <pre><code>$cursor-&gt;match('/^\w+,(.*)\s/');
  </code></pre>
  
  <p>However, this seems to capture the name incorrectly, and links the start of the other name (see below):</p>
  
  <p><a href="https://i.stack.imgur.com/NBDmj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NBDmj.png" alt="enter image description here"></a></p>
  
  <p>Does anyone have any ideas what I could change to match an entire name separated with a comma <strong>and/or space</strong>? I'm awful at writing regex.</p>
  
  <p>Thanks for any help in advance!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here's what should match:</p>
  
  <p>(A name can only be 2 words minimum and 3 words maximum)</p>
  
  <ul>
  <li>Bauman, Steve</li>
  <li>Steve Bauman</li>
  <li>Doe, Mary Sue</li>
  <li>Mary Sue Doe</li>
  </ul>
  
  <p>A last name with a comma will <strong>only</strong> be one word.</p>
  
  <p>What should not match:</p>
  
  <ul>
  <li>Steve</li>
  <li>Bauman</li>
  <li>Mary</li>
  <li>Doe</li>
  </ul>
  
  <p>The regex needs to match an entire name, not single names.</p>
  
  <p>This is the CommonMark code behind the <code>match</code> method:</p>
  
  <pre><code>if (!preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE)) {
      return;
  }
  </code></pre>
  
  <p>This results to:</p>
  
  <pre><code>if (!preg_match('/^\w+,(.*)\s/', "Bauman, Steve", $matches, PREG_OFFSET_CAPTURE)) {
      return;
  }
  </code></pre>