On Sun, Dec 23, 2012 at 8:27 AM, punit jain <[email protected]> wrote:
> Hi,
>
> I am doing grouping but seeing some weird behavior :-
>
> the strings in a file are like :-
> AccessModes =
> (18,Mail,POP,IMAP,PWD,WebMail,WebSite,Relay,Mobile,FTP,MAPI,TLS,LDAP,WebCAL);
> ....
> ...
> .
> multiple lines
>
> I am seeing which lines have both POP and Webmail as below :-
>
> if( $line =~ /AccessModes\s*=\s*.*(WebMail)*.*(POP).*(WebMail)*.*/ ) {
> if(defined $2) {
> print "$2 $1"."$line"."\n";
> }
> }
>
>
> However I get these below :-
>
> POPUse of uninitialized value in concatenation (.) or string at
> test.plline 283, <GEN85> line 2.
> POPUse of uninitialized value in concatenation (.) or string at
> test.plline 283, <GEN86> line 2.
> POPUse of uninitialized value in concatenation (.) or string at
> test.plline 283, <GEN87> line 2.
>
> Any clue why ?
>
> Regards.
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
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/