Hi,
Your basic mistake is making the WebMail strings you are searching for
optional by having an asterisk (*) following them. This means zero or
more instances of the strings. Thus once it matches POP, it is quite
happy to match zero instances of WebMail. I included some code below
to demonstrate:
use strict;
use warnings;
use Data::Dumper;
my @regExps = (
# Original
qr/AccessModes\s*=\s*.*(WebMail)*.*(POP).*(WebMail)*.*/,
# Here we make the second WebMail non-optional
(removed following *)
# so it will match POP with a WebMail later on the line
qr/AccessModes #literal match
\s* # optionasl whitespace
= # literal equal sign
\s* # optional whitespace
.* # match as many characters
(WebMail)* # literal WebMail, but this is optional
# Therefore, it is skipped if POP is found first
.* # match as many characters as we can
(POP) # match literal POP
.* # match as many characters as we can
# until we see WebMail
(WebMail) # match literal WebMail
/x,
# Make the matches non-optional and check for both strings
# at the same time using 'or' (|).
# This is probably the method you want to use.
qr/AccessModes #literal match
\s* # optionasl whitespace
= # literal equal sign
\s* # optional whitespace
.* # match as many characters as we can
# until we find WebMail or POP
(WebMail|POP) # literal WebMail or POP
.* # match as many characters as we can
# non-greedily, so it will match second
# occurence
(WebMail|POP) # literal WebMail or POP
/x
);
while (my $line = <DATA>) {
print $line;
for my $re ( @regExps ) {
print " RE: $re\n";
if( $line =~ /$re/) {
print " 1: >$1<\n" if defined($1);
print " 2: >$2<\n" if defined($2);
print " 3: >$3<\n" if defined($3);
}
}
}
__DATA__
AccessModes = (POP);
AccessModes = (18,Mail,POP,IMAP,PWD,WebMail,WebSite,Relay,Mobile,FTP,MAPI,TLS,LDAP,WebCAL);
AccessModes = (18,Mail,IMAP,PWD,WebMail,WebSite,POP,Relay,Mobile,FTP,MAPI,TLS,LDAP,WebCAL);
HTH, Ken