Link to home
Create AccountLog in
Avatar of bt707
bt707Flag for United States of America

asked on

Perl - regex in an array

I have some phone numbers such as on example below.

These phone numbers must have one of the tags in the numbers that is listed in the array @tags.

What I need to do is to check the number to see if one of the tags in @tags is found in the string for $no, the problem I have is that for the element in @tags that shows as "xx only" that can be any two letter country code, which I have just over 100 of them so do not want to list all of them in the array.

for the "XX only in @tags it's ok to just use any two characters and only, I'm trying to figure out how I can put a pattern in the array to replace the "XX only" so that value can be any two characters followed by the work only.




$no = '+1 800 880 9999 (US only)';


  @tags = ('(local only)',
                '(internal)',
                '(N8 only)',
                '(E8 only)',
                '(S8 only)',
                '(internal only)',
                '(XX only)',
          );

So I can do a foreach loop on each element of @tags to see if it is found in $no, but I'm stuck on how to match the "XX only" which can be any two characters followed by only.

is there anyway that can be done?
ASKER CERTIFIED SOLUTION
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of bt707

ASKER

Thanks to both of you, I did not realize I could do that inside of the array.

Thanks!!!
Either are just as valid e.g.

  $no = '+1 800 880 9999 (US only)';


  @tags = ('(local only)',
                '(internal)',
                '(N8 only)',
                '(E8 only)',
                '(S8 only)',
                '(internal only)',
                '([a-zA-Z][a-zA-Z] only)',
                '(\w\w only)',
          );


 foreach (@tags) {

        print "$_\n" if ($no =~ /$_/);
 }

Open in new window


Gives:

# perl test.pl
([a-zA-Z][a-zA-Z] only)
(\w\w only)
#

Open in new window

Avatar of bt707

ASKER

arober11,  Thanks for the info, appreciate it!!