[英]Regex to match full name with comma or space
I'm working with CommonMark and I've created a '@mention' parser that will return a link when an @mention is found. 我正在使用CommonMark,并且创建了一个'@mention'解析器,当找到@mention时它将返回链接。 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) (一个名称最多只能包含2个单词,最多3个单词)
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: 这是match
方法背后的CommonMark代码:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.