正则表达式设置名字和姓氏中的最大字母数

时间:2017-05-25 06:37:10

标签: php regex

在表格中,我要求用户插入他/她的名字和姓氏,他们之间只有一个空格,并且前后没有空格。

我找到了以下代码:^[a-zA-Z]+ [a-zA-Z]+$

但我希望在第一个中加上 15个字母的限制,并且在姓氏中最多20个字母

1 个答案:

答案 0 :(得分:6)

使用limiting quantifier

$this->db->where('comments.product_id', $product_id);
        $this->db->select('comments.comment,comments.ratings,comments.comment_users_id,comment_users.name');
        $this->db->from('comments');
        $this->db->join('comment_users', 'comment_users.id = comments.comment_users_id');
        $this->db->join('product', 'product.product_id=comments.product_id');
        $this->db->order_by('comments.id', 'DESC');     
        return $this->db->get()->result();

^[a-zA-Z]{1,15} [a-zA-Z]{1,20}$ ^^^^^ ^^^^^^ 限制量词告诉引擎匹配与位于其左侧的子模式匹配的1到15个字符。

更多来自文档:

  

语法为 {min,max} ,其中 min 为零或表示最小匹配数的正整数, max 是等于或大于 min 的整数,表示最大匹配数。如果逗号存在但省略 max ,则最大匹配数为无限。因此{1,15}{0,1}相同,?{0,}相同,*{1,}相同。省略逗号和最大值会告诉引擎完全 min 次重复令牌。