Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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;

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

Report this thread to moderator Post Follow-up to this message
Old Post
Christopher Spears
08-21-07 09:01 AM


Re: entering regular expressions from the keyboard

-----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/

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Pang
08-21-07 09:01 AM


Re: entering regular expressions from the keyboard
Christopher 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

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
08-21-07 09:01 AM


Re: entering regular expressions from the keyboard
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
08-21-07 12:59 PM


Re: entering regular expressions from the keyboard
Jeff 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."


Report this thread to moderator Post Follow-up to this message
Old Post
Dr.Ruud
08-22-07 02:59 AM


Re: entering regular expressions from the keyboard
T24gOC8yMy8wNywgSmF5IFNhdmFnZSA8ZGFnZ2Vy
cXVpbGxAZ21haWwuY29tPiB3cm90ZToKPiBU
 aGF0IG1lYW5zIHRoZSByZWdleCBpcyBhY3R1YWxs
eSBiZWluZyBjb21waWxlZCB0d2ljZS4gSXQg
 cHJvYmFibHkKPiBkb2Vzbid0LCB0aG91Z2gsIG1h
a2Ugc2Vuc2UgdG8gY29tcGlsZSB0aGUgcmVn
 ZXggYmVmb3JlIGVudGVyaW5nIHRoZQo+IGxvb3As
IHNvIHBlcmhhcHMgc29tZXRoaW5nIGxpa2U6
 CgpNYWtlIHRoYXQgKmRvZXMqIG1ha2Ugc2Vuc2Uu
CgotLSBqCi0tLS0tLS0tLS0tLS0tLS0tLS0t
 LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
ClRoaXMgZW1haWwgYW5kIGF0dGFjaG1lbnQo
 cyk6IFsgIF0gYmxvZ2FibGU7IFsgeCBdIGFzayBm
aXJzdDsgWyAgXQpwcml2YXRlIGFuZCBjb25m
 aWRlbnRpYWwKCmRhZ2dlcnF1aWxsIFthdF0gZ21h
aWwgW2RvdF0gY29tCmh0dHA6Ly93d3cudHVh
 dy5jb20gIGh0dHA6Ly93d3cuZG93bmxvYWRzcXVh
ZC5jb20gIGh0dHA6Ly93d3cuZW5nYXRpa2ku
 b3JnCgp2YWx1ZXMgb2Yg4iB3aWxsIGdpdmUgcmlz
ZSB0byBkb20hCg==

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
08-24-07 12:00 AM


Re: entering regular expressions from the keyboard
Jay 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."

Report this thread to moderator Post Follow-up to this message
Old Post
Dr.Ruud
08-24-07 09:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.