次のパターンでは、4 ~ 12 文字のユーザー名を使用できます。最初の文字は文字で、ドットとアンダースコアは 1 つだけです。
$pattern = '~^(?=.{4,12}$)[a-z]++[^\W_]*+(_[^\W_]++)?+(?>\.[^\W_]++)?+(?(1)|(_[^\W_]++)?+)$~i';
preg_match と一緒に使用して、ユーザー名を確認できます。
パターン詳細:
~ # pattern delimiter
^ # begining anchor
(?=.{4,12}$) # lookahead assertion: check if the length is between 4 and 12
# characters. It stand for: "followed by between 4 and 12 chars
# and the end of the string
[a-z]++ # one or more letters
[^\W_]*+ # zero or more letters or digits. Since ^ means that the character
# class is negated, _ is excluded with all that is not a part of \w
(_[^\W_]++)?+ # first capturing group with an underscore and one or more
# letters/digits. The group is optional
(?>\.[^\W_]++)?+ # a non capturing group with a dot (optional too)
(?(1) # conditional: if the first capturing group exist there is nothing
| # OR
(_[^\W_]++)?+ # you can have an underscore here (optional)
)
$ # anchor: end of the string
~ # pattern delimiter
i # case insensitive modifier
「ダムユーザー名」については、「ブラックリスト」を作成して後で(または先読みまたは後読みのパターンで)ユーザー名を確認する以外にできることはあまりありませんが、そうではないため、これが非常に役立つかどうかはわかりませんパスワード。