Tetsuo wrote: > > Hey all - I am trying to learn perl and I know that key to this is a deep > understanding of regular expressions. I attempted to write a little tool > that would allow me to test out regular expressions on multiple files (I'm > thinking an email archive search tool at this point). > > In the first incarnation I would test against each line of a file and then > rewrote it so that I can test against all the contents of a file (basically > one big string variable). It works but can anyone tell me what the best way > to report where in the file the match occurs? Also, is there a quicker way > of getting the contents of a file in a string rather than my loop? > > Here is the code: > > #finding regular expressions in perl>> > > print "Enter a regular expression to use \n"; > my $regexp = <STDIN>; > chomp $regexp; > print "Your regular expression is $regexp \n"; > my $s=""; > while(defined($foo = <c:/davidcode/perlbeast/*.secret>)){ #or whatever file > path you want... > $foo =~ s#.*/##; > open(TEST,$foo) || die "cannot open file"; > while(defined($line = <TEST>)){ > $s = $s . $line; > } > if($s =~ /$regexp/){ > print "match \n"; > } > > close(TEST) || die "cannot close file"; > }
Here is one way to do it: #!/usr/bin/perl -w use strict; print 'Enter a regular expression to use: '; chomp( my $regexp = <STDIN> ); print "Your regular expression is $regexp \n"; @ARGV = glob 'c:/davidcode/perlbeast/*.secret'; while ( <> ) { print "Found in file: $ARGV on line: $.\n$_\n" if /$regexp/; close ARGV if eof; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]