a regular expression issue

2008-04-04 Thread icarus
Hi,

I want to display 'canada', 'cane', 'canine, 'ca.e.02'.
Problem: It only displays 'canada'

#!/usr/bin/perl
use strict;
use warnings;

my $file;
my @xfiles;

@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");

foreach $file (@xfiles){

#want canada only for this iteration
if ($file =~ m/^ca/s){
next if $file =~ m/e(\d\d)$/s; #don't want cane02
next if $file =~ m/e.(\d\d)$/s; #don't want ca.e.02
next if $file =~ m/e$/s; #don't want cane, canine
next if $file =~ m/^\.{1}/; #skips .hidden files
print "$file\n";

}

#then want cane, canine, ca.e.02
elsif ($file =~ m/e$/s or $file =~ m/^ca\.+e$/s or $file =~ m/e.(\d\d)
$/s){
next if $file =~ m/^\.{1}/; #skips .hidden files
print "$file\n";
   }
  }


[ no this is *not* homework. ]

Any ideas? Thanks in advance,


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: a regular expression issue

2008-04-04 Thread John W. Krahn

icarus wrote:

Hi,


Hello,


I want to display 'canada', 'cane', 'canine, 'ca.e.02'.
Problem: It only displays 'canada'

#!/usr/bin/perl
use strict;
use warnings;

my $file;
my @xfiles;

@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");


my @xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine", ".hidden");



foreach $file (@xfiles){


foreach my $file ( @xfiles ) {



#want canada only for this iteration
if ($file =~ m/^ca/s){


The /s option affects whether . matches a newline or not.  You are not 
using . in the pattern so you don't need /s:


if ( $file =~ /^ca/ ) {



next if $file =~ m/e(\d\d)$/s; #don't want cane02


You don't use the results of capturing so you don't need the parentheses:

next if $file =~ /e\d\d$/; #don't want cane02



next if $file =~ m/e.(\d\d)$/s; #don't want ca.e.02


The . meta-character will match any character but it looks like you only 
want to match a period character:


next if $file =~ /e\.\d\d$/; #don't want ca.e.02



next if $file =~ m/e$/s; #don't want cane, canine
next if $file =~ m/^\.{1}/; #skips .hidden files


The if block you are in only contains $file that match /^ca/ so it can't 
match both /^ca/ and /^\./ at the same time.




print "$file\n";

}

#then want cane, canine, ca.e.02
elsif ($file =~ m/e$/s or $file =~ m/^ca\.+e$/s or $file =~ m/e.(\d\d)
$/s){


/^ca\.+e$/ will match the string 'ca.e' or 'ca..e' or 'ca...e' or 
'ca...e', etc. but it doesn't look like you have a string which 
matches that pattern.  Perhaps you meant /^ca.+e$/ which will match 
'cane' or 'canine'.  It looks like you don't need parentheses or the /s 
option in any of those:


elsif ( $file =~ /e$/ or $file =~ /^ca.+e$/ or $file =~ /e\.\d\d$/ ) {

This is an elsif block which means that anything in $file at this point 
will *not* match /^ca/ because the previous if block already matched 
those elements.  At this point the only element of @xfiles that gets 
here is '.hidden'.




next if $file =~ m/^\.{1}/; #skips .hidden files
print "$file\n";
   }
  }



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: a regular expression issue

2008-04-04 Thread Wolf Blaum
hi,

#!/usr/bin/perl
use strict;
use warnings;

my $file;
my @xfiles;

@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");

foreach $file (@xfiles){

#want canada only for this iteration
if ($file =~ /(canada)/){print "$file\n  - end first if -  \n";}

#wb: (expression) groups what you want to find - ie dont search for c,a,n,d...
but for the whole word "canada" , no next required since if you match
canada, elsif wil not be executed (thats what else means, right?)

#then want cane, canine, ca.e.02
elsif ($file =~ /(^cane$)|(canine)|(ca.e.02)/){ print "$file\n"; }
  }

#wb: same as above, | lists alternatives, ie either "cane" or "canine" or ..
^ matches beginning of word, $ matches end



Cheers, Wolf


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: a regular expression issue

2008-04-04 Thread John W. Krahn

Wolf Blaum wrote:


#!/usr/bin/perl
use strict;
use warnings;

my $file;
my @xfiles;

@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");

foreach $file (@xfiles){

#want canada only for this iteration
if ($file =~ /(canada)/){print "$file\n  - end first if -  \n";}

#wb: (expression) groups what you want to find - ie dont search for c,a,n,d...
but for the whole word "canada" ,


Parentheses *can* be used for grouping but they are not really needed 
here.  They can also be used for capturing but they are not needed for 
that either.




no next required since if you match
canada, elsif wil not be executed (thats what else means, right?)


Correct.



#then want cane, canine, ca.e.02
elsif ($file =~ /(^cane$)|(canine)|(ca.e.02)/){ print "$file\n"; }
  }

#wb: same as above, | lists alternatives, ie either "cane" or "canine" or ..
^ matches beginning of word, $ matches end


Again, the parentheses are superfluous as they are not needed for 
capturing or grouping.  The ^ (beginning of line) and $ (end of line) 
anchors only apply to the pattern 'cane', the other alternations can 
match anywhere in the string.  The pattern /ca.e.02/ will match 
'ca.e.02' but it will also match 'camel02' because the . meta-character 
will match any character except newline.  If you want to match a literal 
. character then you have to escape it: /ca\.e\.02/.


It looks like you intended to use the pattern:

/^(?:cane|canine|ca\.e\.02)$/

or better:

/^ca(?:ne|nine|\.e\.02)$/



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/