develooper Front page | perl.beginners | Postings from February 2002

RE: pattern matching question

Thread Previous | Thread Next
Jason Larson
February 28, 2002 10:23
RE: pattern matching question
Message ID:
-----Original Message-----
From: richard noel fell []
Sent: Thursday, February 28, 2002 10:38 AM
Subject: pattern matching question

I have the following bit of code:
#!/usr/bin/perl -w
open In2,"/home/rfell/tutoring/beaven/webproject/tmp/maxima_log" or die
"Cannot open maxima_log:$!";
my $Line; 
while (defined($Line=<In2>)){
print "==> $2\n";
#close In2;

maxima_log is the following
(D1) 				    - ----
(D2) 				       0
(D3) 	   /home/rfell/tutoring/beaven/webproject/tmp/maxima_results

I want to match the D lines, so my match pattern is as above. It seems
that I have the following construct:
(D followed by a digit(s)) followed by white space followed by some
characters. I want to capture the last characters. In the above example,
for line (D2), I want to get the "0", which is stored, I hope, in $2.
However, I get no match. If I replace $2 with $1, I do get the (D1), etc
to print.

Another question
1) If I change the match pattern to /(\(D\d+\))\s+(\w*)/, that is, one
or more occurences of white space, I get no match at all. $1 does not
print out now. Do I not really have white space?

Has anyone any idea what I am doing wrong? Thanks for your help to a
perl newcomer.
Dick Fell
Your pattern match /(\(D\d+\))\s*(\w*)/ is not correct.  You stated :"It
seems that I have the following construct: (D followed by a digit(s))
followed by white space followed by some characters."  That is not quite
  Your construct is: (D followed by one or more digits) followed by zero or
more white space characters followed by zero or more **WORD** characters.
Word characters are defined as any letter, number, or the Underscore
character (and possibly additional alphabetic characters defined a specific
locales).  So, your hyphens don't get matched, nor do your slashes.  That is
why your match returns unexpected results.  You should change the last
portion to include other characters.  One option is /(\(D\d+\))\s*(.+)/
which would then match $1 to one or more characters of ANY type.




The information contained in this ELECTRONIC MAIL transmission
is confidential.  It may also be privileged work product or proprietary
information. This information is intended for the exclusive use of the
addressee(s).  If you are not the intended recipient, you are hereby
notified that any use, disclosure, dissemination, distribution [other
than to the addressee(s)], copying or taking of any action because
of this information is strictly prohibited.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About