# SPUG: regular expression difficulty, plus compiled perl

Scott Blachowicz scott at sabmail.rresearch.com
Tue Mar 28 13:39:52 CST 2000

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