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:
$cursor->match('/^\w+,(.*)\s/');
However, this seems to capture the name incorrectly, and links the start of the other name (see below):
Does anyone have any ideas what I could change to match an entire name separated with a comma and/or space? I'm awful at writing regex.
Thanks for any help in advance!
Edit:
Here's what should match:
(A name can only be 2 words minimum and 3 words maximum)
A last name with a comma will only be one word.
What should not match:
The regex needs to match an entire name, not single names.
This is the CommonMark code behind the match
method:
if (!preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE)) {
return;
}
This results to:
if (!preg_match('/^\w+,(.*)\s/', "Bauman, Steve", $matches, PREG_OFFSET_CAPTURE)) {
return;
}
As per your new rules, match a name that has 2 or 3 words and should be separated by spaces ( ) or commas (,)
/\w+([, ]+\w+){1,2}/