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]*(?:-?[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-z](_?[a-z0-9])*$/

ダミーダミー

于 2013-03-05T09:42:17.950 に答える
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 に答える