0

我的模式: ^([((01|02|35|27|09|38|12|32|21|28|26|36|08|20|24|04|34|23|31|07|16|11|18|14|03|22|37|25|06|30|13|19|10|05|29|15|17|33)(\d{7}))]{9}|(\d{12}))$

在下图中,91不在列表中,01|02|35|27|09|38|12|32|21|28|26|36|08|20|24|04|34|23|31|07|16|11|18|14|03|22|37|25|06|30|13|19|10|05|29|15|17|33但它仍然返回。

我该如何纠正?

PHP 实时正则表达式

4

5 回答 5

1

Your entire regex says: "Find 9 of any characters out of 0-9, parentheses, braces or the pipe character - alternatively find 12 digits"

With that it should be fairly obvious what you've done wrong.

Try this regex instead:

^(?:(?:01|02|35|.....|33)\d{10}|\d{12})$
于 2013-06-06T01:30:32.397 回答
1

You have put the whole first section of your regex in a character class (the square brackets, everything between [ and ]. You don't need / want a character class as now you are only matching on length (all numbers are included in your char class).

So you can probably use something like (untested):

^((?:01|02|35|27|09|38|12|32|21|28|26|36|08|20|24|04|34|23|31|07|16|11|18|14|03|22|37|25|06|30|13|19|10|05|29|15|17|33)(?:\d{7}))|(\d{12})$

This should return all numbers of 12 characters (second section) or all numbers of 9 characters starting with the supplied sequences. If that is what you need...

于 2013-06-06T01:31:40.867 回答
0

由于您交替使用从 01 到 38 的所有数字,因此您可以使用以下命令:

^(?:(?:0[1-9]|[12]\d|3[0-8])\d{7}|\d{12})$
于 2013-06-06T05:45:28.220 回答
0

感谢 Kollink,我终于创建了对我来说完美的模式:

^(?:(?:01|02|35|27|09|38|12|32|21|28|26|36|08|20|24|04|34|23|31|07|16|11|18|14|03|22|37|25|06|30|13|19|10|05|29|15|17|33)\d{7}|\d{12})$

于 2013-06-06T01:49:03.720 回答
0

仅仅检查前两个字符不是我会使用正则表达式的东西:

$valid = ['01', '02', '35', '27', '09', '38', ...];

if (in_array(substr($str, 0, 2), $valid)) {
    // continue validation here
}
于 2013-06-06T01:39:47.810 回答