# SPUG: regular expression difficulty, plus compiled perl

Todd Wells toddw at wrq.com
Tue Mar 28 14:02:20 CST 2000

Aha!  I previously understood all of this (yes I know about $1,$2, etc...)
except for the part about "the return value of that matching expression (in
a list context) will be the list of matching subexpressions" -- now I see
the light!  I was expecting the return value of the matching expression to
be the matching expression!

-----Original Message-----
From: Scott Blachowicz [mailto:scott at sabmail.rresearch.com]
Sent: Tuesday, March 28, 2000 11:40 AM
To: Todd Wells
Cc: 'Ryan Erwin'; spug-list at pm.org
Subject: Re: SPUG: regular expression difficulty, plus compiled perl

On Tue, Mar 28, 2000 at 11:27:55AM -0800, Todd Wells wrote:
> Thanks again too all of you who've offered solutions...
>
> So Ryan, it would appear that the difference here is the parentheses
inside
> the regular expression?  Why do the parentheses make it work?  I must
> confess some puzzlement over this.
>
> My original that didn't work:
> ($test1 =$0)=~ m#.*\\#;

What you're saying there is to assign $0 to the variable$test1, then
test to see if it has a backslash in it.

> Yours that does work:
> $test =~ m#(.*\\)#;. The parens actually capture part of the matching string. To get at the captured substrings, use$N (where N is a number corresponding to the
order of appearance of the open paren in the pattern).  Also, the
return value of that matching expression (in a list context) will be
the list of matching subexpressions. So, for example,

$test = 'c:\\foo\\bar\\baz.cpp'; ($WholePath, $DirWithSlash,$Dir, $Base) = ($test =~ m#(((.*)\\)(.*))#);
print "$WholePath =$1\n";
print "$DirWithSlash =$2\n";
print "$Dir =$3\n";
print "$Base =$4\n";

prints out this:

c:\foo\bar\baz.cpp = c:\foo\bar\baz.cpp
c:\foo\bar\ = c:\foo\bar\
c:\foo\bar = c:\foo\bar
baz.cpp = baz.cpp

Scott

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
Email to majordomo at pm.org: "action" spug-list your_address