Code Comments
Programming Forum and web based access to our favorite programming groups.Hi!
I'm trying to get back into Perl again by working
through Intermediate Perl. Unfortunately, the Perl
part of my brain has atrophied!
I'm working on the second exercise of the second
chapter. I'm supposed to write a program that asks
the user to type a regular expression. The program
then uses the regular expression to try to find a
match in the directory that I hard coded into the
program. Here is what I have so far:
#!/usr/bin/perl -w
use strict;
print "Enter regular expression: ";
chomp(my $regexp = <STDIN> );
#print $regexp;
opendir(CPPDIR,"/home/io/chris_cpp/") or die "Could
not open directory: $!";
my @allfiles = readdir CPPDIR;
closedir CPPDIR;
foreach $_(@allfiles){
if ($_ =~ \$regexp){
print $_."\n";
}
}
My problem lies with the matching part. I'm not sure
how to use the string that I stored in the $regexp
variable as a regular expression. Any hints?
"I'm the last person to pretend that I'm a radio. I'd rather go out and be
a color television set."
-David Bowie
"Who dares wins"
-British military motto
"I generally know what I'm doing."
-Buster Keaton
Post Follow-up to this message-----Original Message----- >From: Christopher Spears <cspears2002@yahoo.com> >Sent: Aug 21, 2007 11:28 AM >To: beginners@perl.org >Subject: entering regular expressions from the keyboard > >Hi! > >I'm trying to get back into Perl again by working >through Intermediate Perl. Unfortunately, the Perl >part of my brain has atrophied! > >I'm working on the second exercise of the second >chapter. I'm supposed to write a program that asks >the user to type a regular expression. The program >then uses the regular expression to try to find a >match in the directory that I hard coded into the >program. Here is what I have so far: > >#!/usr/bin/perl -w >use strict; > >print "Enter regular expression: "; > >chomp(my $regexp = <STDIN> ); >#print $regexp; $regexp = quotemeta($regexp); See also perldoc -f quotemeta. -- Jeff Pang - pangj@earthlink.net http://home.arcor.de/jeffpang/
Post Follow-up to this messageChristopher Spears wrote:
> Hi!
>
> I'm trying to get back into Perl again by working
> through Intermediate Perl. Unfortunately, the Perl
> part of my brain has atrophied!
>
> I'm working on the second exercise of the second
> chapter. I'm supposed to write a program that asks
> the user to type a regular expression. The program
> then uses the regular expression to try to find a
> match in the directory that I hard coded into the
> program. Here is what I have so far:
>
> #!/usr/bin/perl -w
> use strict;
>
> print "Enter regular expression: ";
>
> chomp(my $regexp = <STDIN> );
> #print $regexp;
>
# from here
> opendir(CPPDIR,"/home/io/chris_cpp/") or die "Could
> not open directory: $!";
> my @allfiles = readdir CPPDIR;
> closedir CPPDIR;
# try:
my @allfiles = glob( '*' );
>
> foreach $_(@allfiles){
> if ($_ =~ \$regexp){
# bad regular expression. try:
if( /$regexp/ ){
> print $_."\n";
> }
> }
>
> My problem lies with the matching part. I'm not sure
> how to use the string that I stored in the $regexp
> variable as a regular expression. Any hints?
>
>
>
> "I'm the last person to pretend that I'm a radio. I'd rather go out and b
e a color television set."
> -David Bowie
>
> "Who dares wins"
> -British military motto
"Of course we'll win; we're British"
- another British military motto
>
> "I generally know what I'm doing."
> -Buster Keaton
>
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing th
em."
Aristotle
Post Follow-up to this messageOn Aug 20, 11:28 pm, cspears2...@yahoo.com (Christopher Spears) wrote:
> I'm working on the second exercise of the second
> chapter. I'm supposed to write a program that asks
> the user to type a regular expression. The program
> then uses the regular expression to try to find a
> match in the directory that I hard coded into the
> program. Here is what I have so far:
>
> #!/usr/bin/perl -w
> use strict;
>
> print "Enter regular expression: ";
>
> chomp(my $regexp = <STDIN> );
> #print $regexp;
>
> opendir(CPPDIR,"/home/io/chris_cpp/") or die "Could
> not open directory: $!";
> my @allfiles = readdir CPPDIR;
> closedir CPPDIR;
>
> foreach $_(@allfiles){
> if ($_ =~ \$regexp){
> print $_."\n";
> }
>
> }
>
> My problem lies with the matching part. I'm not sure
> how to use the string that I stored in the $regexp
> variable as a regular expression. Any hints?
Shawn and Jeff each gave you half of the answer.
Jeff pointed out that when your pattern is contained in a variable,
you should use quotemeta(). This will backslash any metacharacters
the variable might contain, so that they match themselves rather than
being "special" in the pattern match (so any periods match periods,
rather than "any character", plus signs match plus signs, rather than
meaning "one or more of the previous", etc):
$regexp = quotemeta($regexp)
And Shawn pointed out that the proper syntax for a pattern match is:
$_ =~ /$regexp/
Those two lines should be combined:
$regexp = quotemeta($regexp);
foreach $_(@allfiles){
if ($_ =~ /$regexp/){
print $_."\n";
}
}
Or, instead of calling quotemeta() explicitly, you can use the \Q and
\E escape sequences to do the backquoting within the pattern match
itself:
foreach $_ (@allfiles) {
if ($_ =~ /\Q$regexp\E/) {
print $_ . "\n";
}
}
Also note that an experienced Perl programmer would either eliminate
the $_ whenever it's not needed:
foreach (@allfiles) {
if (/\Q$regexp\E/) {
print "$_\n";
}
}
Or would use a better variable name as the loop iterator:
foreach my $file (@allfiles) {
if ($file =~ /\Q$regexp\E/) {
print "$file\n";
}
}
Hope that helps,
Paul Lalli
Post Follow-up to this messageJeff Pang schreef: > Christopher Spears: > > $regexp = quotemeta($regexp); Since it specifically asks for a regular expression, I would definitely not do quotemeta(). Make that print qr/$regexp/; -- Affijn, Ruud "Gewoon is een tijger."
Post Follow-up to this messageT24gOC8yMy8wNywgSmF5IFNhdmFnZSA8ZGFnZ2Vy cXVpbGxAZ21haWwuY29tPiB3cm90ZToKPiBU aGF0IG1lYW5zIHRoZSByZWdleCBpcyBhY3R1YWxs eSBiZWluZyBjb21waWxlZCB0d2ljZS4gSXQg cHJvYmFibHkKPiBkb2Vzbid0LCB0aG91Z2gsIG1h a2Ugc2Vuc2UgdG8gY29tcGlsZSB0aGUgcmVn ZXggYmVmb3JlIGVudGVyaW5nIHRoZQo+IGxvb3As IHNvIHBlcmhhcHMgc29tZXRoaW5nIGxpa2U6 CgpNYWtlIHRoYXQgKmRvZXMqIG1ha2Ugc2Vuc2Uu CgotLSBqCi0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t ClRoaXMgZW1haWwgYW5kIGF0dGFjaG1lbnQo cyk6IFsgIF0gYmxvZ2FibGU7IFsgeCBdIGFzayBm aXJzdDsgWyAgXQpwcml2YXRlIGFuZCBjb25m aWRlbnRpYWwKCmRhZ2dlcnF1aWxsIFthdF0gZ21h aWwgW2RvdF0gY29tCmh0dHA6Ly93d3cudHVh dy5jb20gIGh0dHA6Ly93d3cuZG93bmxvYWRzcXVh ZC5jb20gIGh0dHA6Ly93d3cuZW5nYXRpa2ku b3JnCgp2YWx1ZXMgb2Yg4iB3aWxsIGdpdmUgcmlz ZSB0byBkb20hCg==
Post Follow-up to this messageJay Savage schreef: > Dr.Ruud: > > Not sure where your headed with this. My headed? :) It was an alternative for the commented "debug" line. > First, OP wants to print the input back to the user. And I presume that it is more a developer directed print statement. -- Affijn, Ruud "Gewoon is een tijger."
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.