2011-03-09 18 views
1

PHP regular expression to match alpha-numeric strings with some (but not all) punctuationをフォローアップするには、数字、文字または句読点のうちの少なくとも2つの文字を受け入れる必要があります。長さは18文字です。これはこれを行う最良の方法ですか?私はRegExLibのパターンを使ってこの正規表現を作った。PHPの正規表現:文字列に文字列を含める必要があります

preg_match('@' . 
// one number and one letter 
'^(?=.*\d)(?=.*[a-zA-Z])[!-%\'-?A-~]{6,18}$' . 
// one number and one punctuation 
'|^(?=.*\d)(?=.*[!-%\'-/:-?\[-`{-~])[!-%\'-?A-~]{6,18}$' . 
// or one punctation and one 
'|^(?=.*[!-%\'-/:-?\[-`{-~])(?=.*[a-zA-Z])[!-%\'-?A-~]{6,18}$' . 
'@i', $pass, $matches); 

答えて

4

解決策が複雑すぎます。 char型と長さの存在をチェックするだけです。

<?php 
$is_special = preg_match('/[+#!\?]/', $pass); //sample only 
$is_numeric = preg_match('/[0-9]/', $pass); 
$is_char = preg_match('/[a-zA-Z]/', $pass); 

if ($is_special + $is_numeric + $is_char < 2) { 
    //fail 
} 

//+lenght check 
1

3つのタイプをすべてフィードすると、正規表現は機能しません。拒否されます。あなたは組み合わせのあらゆる可能性を考慮して必要があるので、1は非常に冗長になる構築するには

: - 桁

  • 桁 - レター
  • 手紙 - 句読点
  • 句読点 -

    1. 手紙手紙
    2. 数字 - 句読点
    3. 句読点 - 数字
    4. 手紙 - 数字 - 句読点
    5. 手紙 - 句読点 - 桁
    6. 桁 - 手紙 - 句読点
    7. 桁 - 句読点 - レター
    8. 句読点 - 手紙 - 桁
    9. 句読点 - 数字 - レター

    代わりに手動で行うことをおすすめします。

    function isValidString($string) { 
        $count = 0; 
        //Check for the existence of each type of character: 
        if (preg_match('/\d/', $string)) { 
         $count++; 
        } 
        if (preg_match('/[a-z]/i', $string)) { 
         $count++; 
        } 
        if (preg_match('/[!-%\'-\/:-?\[-{-~]/', $string)) 
         $count++; 
        } 
        //Check the whole string 
        $regex = '/^[a-z\d!-%\'-\/:-?\[-{-~]{6,18}$/'; 
        if ($count >= 2 && preg_match($regex, $string)) { 
         return true; 
        } 
        return false; 
    } 
    

    あなたの正規表現と同じくらいの時間で、それはもっと読みやすくなります(IMHO)...

  • +0

    が含まれている場合、私はすべてを私の正規表現をテストした小資本の数字と特殊文字に チェックが可能3つのタイプとそれはまだ働いた。先読みによっても注文が処理されます。しかし、私はあなたのものがより読みやすく、動作する方法を見ます。 – nymo

    1

    erenonの解決策は、間違ってスペースを許しています。 (長さをチェックするときに有効な文字をチェックする必要があります)。 は、ここで私はそれを行うだろう方法は次のとおりです。

    if (preg_match('& 
        # Password with 6-18 chars from 2 of 3 types: (digits|letters|punct). 
        ^       # Anchor to string start. 
        (?:       # Non-capture group for alternatives. 
         (?=.*?[0-9])    # Either a digit 
         (?=.*?[a-zA-Z])   # and a letter, 
        | (?=.*?[0-9])    # Or a digit 
         (?=.*?[!-%\'-/:-?[-`{-~]) # and a punctuation, 
        | (?=.*?[!-%\'-/:-?[-`{-~]) # Or a punctuation 
         (?=.*?[a-zA-Z])   # and a letter. 
        )       # End group of alternatives. 
        [!-%\'-?A-~]{6,18}$   # Match between 6 and 18 valid chars. 
        &x', $password)) { 
        // Good password 
    } else { 
        // Bad password 
    } 
    

    注長さ基準は一度だけチェックする必要があるということ。また、それはこれまでの他の解決策より速い可能性があります。ここで

    +0

    パスワードでスペースが有効でないのはなぜですか? – erenon

    +0

    長さをチェックするのに使用されたオリジナルポストの正規表現文字クラスにはスペースは含まれていません。しかし、あなたが正しいことは、これが質問の言葉で明白に綴られていないということです。 – ridgerunner

    1

    は、それが1つの正規表現である:

    /^(?=.*[A-Za-z])(?=.*[0-9])([[email protected]#$%^&*-\[\]])+$/

    は、パスワードは、少なくとも1つの文字と1つの番号

    関連する問題