我正在尝试为preg匹配构建这些正则表达式语句,但我遇到了很多麻烦。有人可以找出任何错误并提供正确的preg_match代码。
组名称必须介于2到20个字符之间,并且包含大写和小写字母
群组大小必须是2到6之间的数字
邮政编码需要是有效的英国邮政编码(政府提供的声明,但不确定是否正确实施)
预算必须是1到10000之间的数字
if (!preg_match('/^[a-zA-Z \']+$/', $group_name)) {
unsuccessful("Please ensure that group name contains only letters, apostrophes
and spaces and is between 2 and 20 characters long",
$group_name, $group_size, $postcode);
} else if (!preg_match('/^([2-6])$/', $group_size)) {
unsuccessful("Please ensure that group size is between 2 and 6 people",
$group_name, $group_size, $postcode);
} else if (!(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode)
|| preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode)
|| preg_match("/^GIR0[A-Z]{2}$/",$postcode))) {
unsuccessful("Please ensure that the postcode is correct",
$group_name, $group_size, $postcode);
} else if (!preg_match('/^([1-9][0-9][0-9]{0,2}|10000)$/', $budget)) {
unsuccessful("Please ensure budget is between 1 and 10000 and contains only numbers",
$group_name, $group_size, $postcode); if (!preg_match('/^[a-zA-Z \']+$/', $group_name)) {
unsuccessful("Please ensure that group name contains only letters
and spaces and is between 2 and 20 characters long",
$group_name, $group_size, $postcode);
} else if (!preg_match('/^([2-6])$/', $group_size)) {
unsuccessful("Please ensure that group size is between 2 and 6 people",
$group_name, $group_size, $postcode);
} else if (!(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode)
|| preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode)
|| preg_match("/^GIR0[A-Z]{2}$/",$postcode))) {
unsuccessful("Please ensure that the postcode is correct",
$group_name, $group_size, $postcode);
} else if (!preg_match('/^([1-9][0-9][0-9]{0,2}|10000)$/', $budget)) {
unsuccessful("Please ensure budget is between 1 and 10000 and contains only numbers",
$group_name, $group_size, $postcode);
非常感谢
卡比尔
答案 0 :(得分:1)
群组名称必须介于2到20个字符之间,并且包含大写和小写字母
/^[a-zA-Z \']{2,20}$/
群组大小必须是2到6之间的数字
/^([2-6])$/
邮政编码需要是有效的英国邮政编码
/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/
预算必须是1到10000之间的数字
/^([1-9]|[1-9][0-9][0-9]{0,2}|10000)$/