简体   繁体   English

PHP正则表达式的5个字符的随机模式

[英]PHP regex random pattern of 5 characters

I am trying to check a license key with a pattern of 5 groups of 5 random letters separated by "-". 我正在尝试使用5组5个随机字母(用“-”分隔)的模式检查许可证密钥。 I would like to verify the following: each group consists of 5 letters, each group does not contact certain letters, and all of the groups are capital letters. 我想验证以下内容:每个组由5个字母组成,每个组不联系某些字母,并且所有组都是大写字母。 I know I can do this with the following, but I believe there is an easier method with a possible single regex expression: 我知道我可以使用以下方法做到这一点,但我相信有一个可能使用单个正则表达式的简单方法:

$testKey = 'ASDFG-ASDFG-ASDFG-ASDFG-ASDFG';
$testKeyArray = explode('-', $testKey);
$cntKey = count($testKeyArray);
if($cntKey != 5) {
    echo 'Failed';
} else {
    for($x=0;$x<$cntKey;++$x) {
        if(strlen($testKeyArray[$x]) != 5) {
            echo 'Failed';
        }
        if(preg_match("^[B]^",$testKeyArray[$x]) == 1) {
            echo 'Failed';
        }
    }
    echo 'Success';
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

Whereas

^\w{5}-\w{5}-\w{5}-\w{5}-\w{5}$

Will match any alphunumeric characters AND underscores, I believe you want to match just alphabetical characters: 将匹配所有字母数字字符和下划线,我相信您只想匹配字母字符:

all of the groups are capital letter 所有组都是大写字母

In that case use 在那种情况下使用

/^[A-Z]{5}-[A-Z]{5}-[A-Z]{5}-[A-Z]{5}-[A-Z]{5}$/

Example: 例:

$pattern = "/^[A-Z]{5}-[A-Z]{5}-[A-Z]{5}-[A-Z]{5}-[A-Z]{5}$/";
if (preg_match($pattern, "ASDFG-ASDFG-ASDFG-ASDFG-ASDFG")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

each group does not contact certain letters 每个小组不联系某些信件

If you want it to not contain specific letters, you will need to break up your ranges. 如果您不希望它包含特定字母,则需要打破范围。 For example, if you don't want the letter C , your range would be [A-BD-Z] instead of [AZ] 例如,如果您不希望字母C ,则范围将是[A-BD-Z]而不是[AZ]

Here is a fairly easy way. 这是一个相当简单的方法。 Captures and validates in one operation. 一次操作即可捕获并验证。

2 ways. 2种方式。

  • This way is the quickest for just simple class exclusions. 对于简单的类排除,这种方式是最快的。

^(?!.*[B])([AZ]{5})-([AZ]{5})-([AZ]{5})-([AZ]{5})-([AZ]{5})$

Expanded: 扩展:

 ^                             # BOS
 (?! .* [B] )                  # Not any of these, add more
 ( [A-Z]{5} )                  # (1)
 -
 ( [A-Z]{5} )                  # (2)
 -
 ( [A-Z]{5} )                  # (3)
 -
 ( [A-Z]{5} )                  # (4)
 -
 ( [A-Z]{5} )                  # (5)
 $                             # EOS 
  • This way is the quickest for complex expressions. 对于复杂的表达式,这种方式是最快的。

^((?&ltr){5})-((?&ltr){5})-((?&ltr){5})-((?&ltr){5})-((?&ltr){5})$(?(DEFINE)(?<ltr>(?![B])[AZ]))

Expanded: 扩展:

 ^                             # BOS
 ( (?&ltr){5} )                # (1)
 -
 ( (?&ltr){5} )                # (2)
 -
 ( (?&ltr){5} )                # (3)
 -
 ( (?&ltr){5} )                # (4)
 -
 ( (?&ltr){5} )                # (5)
 $                             # EOS 

 (?(DEFINE)
      (?<ltr>                       # (6 start)
           (?! [B] )                     # Not any of these chars ahead, add more
           [A-Z]                         # Single any of A-Z
      )                             # (6 end)
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM