at lest 2 character in that and a maximum of 3 characters.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KUTTAN
    New Member
    • Mar 2007
    • 33

    at lest 2 character in that and a maximum of 3 characters.

    I am creating a 6 character alpha numeric sting dynamically

    i just want to esure that there is at lest 2 character in that and a maximum of 3 characters.
    others will be automatically digits (3-4)

    I made a regular expression ie like


    ^([a-zA-Z]{2,3}[0-9]{3,4})|([0-9]{3,4}[a-zA-Z]{2,3}?)$

    but is not matching for 22XX22
    here i have 4 digits and 2 letters it must be matching
    what to do?

    KUTTAN
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Your description of how the string can be constructed is not very clear so unless you can define the possible permuations better here is a generic way to check such a string:

    Code:
    my $string = '22XX22';
    if ($string =~ /[a-zA-Z]{2,3}/ && $string =~ /^[a-zA-Z0-9]{6}$/) {
       print "$string is good\n";
    }
    else {
       print "$string is bad\n";
    }

    Comment

    • KUTTAN
      New Member
      • Mar 2007
      • 33

      #3
      X88X88 should be matched for the regular expression [a-zA-Z]{2,3}, since there are two characters
      but this is not matching
      :(

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        No, it should not match. You don't understand how {n,n} works. It matches if the characters are in sequence, so XX and XXX will match but not X because it is only one character. Here is a solution that will work no matter where the alpha characters are in the string:

        Code:
        my $string = 'X88X88';
        my $alphas = $string =~ tr/a-zA-Z/a-zA-Z/;#counts the number of characters
        if (($alphas == 2 || $alphas == 3) && $string =~ /^[a-zA-Z0-9]{6}$/) {
           print "$string is good\n";
        }
        else {
           print "$string is bad\n";
        }

        Comment

        Working...