3

我需要验证一个可以包含小写字符和破折号但不能在开头、结尾或重复的字符串。然后也可以有数字,但不是在开始时。

这是它应该如何工作的:

Match: ab9cd-a9bc-abbbc95
Not match: 5abcd
Not match: abbcd-
Not match: -abcd
Not match: abcd--abcd

除了最后一种情况,我已经能够做所有事情,重复的破折号不应该匹配。

我有这个:

/^[a-z]+[a-z0-9\-]+[a-z0-9]$/

我尝试了这个,但没有按预期工作:

/^[a-z]+[a-z0-9\-?]+[a-z0-9]$/
4

4 回答 4

1

另一种方法:

^[a-z](?:[a-z\d]|[a-z\d]\-)*[a-z\d]+$

在这里解释演示:http ://regex101.com/r/mE8pB8

于 2013-03-05T10:05:15.110 回答
0
/^[a-z](_?[a-z0-9])*$/

假人

于 2013-03-05T09:42:17.950 回答
0

怎么样:

^[a-z][a-z0-9]*(?:-?[a-z0-9]+)*[a-z0-9]$

例子:

$arr = array('ab9cd-a9bc-abbbc95','5abcd','abbcd-','-abcd','abcd--abcd');
foreach($arr as $str) {
    if (preg_match('/^[a-z][a-z0-9]*(?:-?[a-z0-9]+)*[a-z0-9]$/', $str)) {
        echo "OK : $str\n";
    } else {
        echo "KO : $str\n";
    }
}

输出:

OK : ab9cd-a9bc-abbbc95
KO : 5abcd
KO : abbcd-
KO : -abcd
KO : abcd--abcd

解释:(来自 YAPE::Regex::Explain)

The regular expression:

(?-imsx:^[a-z][a-z0-9]*(?:-?[a-z0-9]+)*[a-z0-9]$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  [a-z0-9]*                any character of: 'a' to 'z', '0' to '9'
                           (0 or more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    -?                       '-' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    [a-z0-9]+                any character of: 'a' to 'z', '0' to '9'
                             (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  [a-z0-9]                 any character of: 'a' to 'z', '0' to '9'
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-03-05T09:48:18.573 回答
0

我希望这个能解决你的问题

/^[a-z0-9]+((\-{0,1})[a-z0-9]+)+$/

编辑:

以下更适合

/^[a-z]+[a-z0-9]+((\-{0,1})[a-z0-9]+)+$/
于 2013-03-05T09:56:15.947 回答