首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匹配2个字符和2到10个数字的preg_match正则表达式

匹配2个字符和2到10个数字的preg_match正则表达式
EN

Stack Overflow用户
提问于 2014-05-04 13:15:54
回答 1查看 1.8K关注 0票数 0

我需要做一个正则表达式,如果字符串中的前两个字符是2个字母,剩下的只有数字,长度为2-10,则返回true。

我无法尝试。我只是不知道该怎么做。

这就是前一个家伙留给我的东西:

代码语言:javascript
复制
function clearTerm($term) {
    if( preg_match('/^[a-z0-9()\- ]+$/i', $term) ) {
        return true;
    } else {
        return false;
    }
}

这是我试过的,但没有希望。我不知道怎么检查前两个,然后再检查其余的。

代码语言:javascript
复制
function clearTerm($term) {
    if( preg_match('/^([a-z0-9]+$/i{2})', $term) ) {
        return true;
    } else {
        return false;
    }
}

regex需要返回true if:前两个字符是两个小写字母(lg),其余是数字,长度为2到10个数字。

所以,lg01234 -> True, lgx1 -> False

试过但失败了,所以这里问。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-04 13:20:31

代码语言:javascript
复制
function clearTerm($term) {
    if( preg_match('/^[a-z]{2}[0-9]{2,10}$/', $term) ) {
        return true;
    } else {
        return false;
    }
}


NODE                       EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-z]{2}                 any character of: 'a' to 'z' (2 times)
--------------------------------------------------------------------------------
  [0-9]{2,10}              any character of: '0' to '9' (between 2 and 10 times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the string
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23456688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档