这些preg_match语句是否正确? [关闭]

I am trying to build these regex statements for the preg matches but I am having a lot of trouble. Could someone please identify any that are wrong and provide the correct preg_match code.

Group name needs to be between 2 and 20 characters and contain uppercase and lowercase letters

Group size needs to be a number between 2 and 6

Postcode needs to be a valid uk postcode (government provided statement but unsure if correct implementation)

Budget needs to be a number between 1 and 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);

Thanks a lot

Kabeer

Group name needs to be between 2 and 20 characters and contain uppercase and lowercase letters

  • Create a range match between 2 and 20 of the preceding token.

/^[a-zA-Z \']{2,20}$/

Group size needs to be a number between 2 and 6

  • This one was fine

/^([2-6])$/

Postcode needs to be a valid uk postcode

  • According to regexlib, this will match UK postcodes

/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/

Budget needs to be a number between 1 and 10000

/^([1-9]|[1-9][0-9][0-9]{0,2}|10000)$/