----- Original Message ----
From: Ovid <[EMAIL PROTECTED]>
To: beginners@perl.org
Sent: Sunday, January 21, 2007 10:26:20 PM
Subject: Re: putting ";" as a replacement in the substitution.

--- Michael Alipio <[EMAIL PROTECTED]> wrote:

<snip>

>  No, not correct.  The regular expression is what's being matched.  Period. 
>  The capturing parentheses merely capture some of all of the regular
>  expression into a 'dollar digit' variable ($1, $2, and so on).

>  So for this:

>    $var =~ s/foo(bar)/$1/;

>  The 'foo(bar)' is what is being matched and the 'bar' is captured to the $1
>  variable.  For this:

>    if ( $var =~ /foo(bar)/ ) { ... }

>  The 'foo(bar)' is *still* what is being matched and the 'bar' is *still* what
>  is being captured to the $1 variable.  

>  The first version is when you want to alter the string you're matching.  The
>  second version is good when you want to take action based upon a match and
>  possibly extract data out of the string.

I see.. Now it's a lot more clearer.

It would be pointless to put () in my regexp when testing with if, unless I'm 
grouping something or I want to do something with $1. 

if  /(^\w+)\s+/

But if I am assigning something, like:

my $captured =~ /^(\w+)\s+/

I should put it inside parenthesis.
I also noticed that $capture here will always contain the first catched match 
($1).

The, (?:) as suggested by someone is also good when I want to avoid something 
being stored in $n... I have read about it and a lot more(particularly the 
extended regexp features) in perlre but not quite sure what they mean. The 
topic on backtracking when using quantifier is also a good read.

Anyway, thanks for your help!
Have a nice day!


>  Cheers,
>  Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/









 
____________________________________________________________________________________
Food fight? Enjoy some healthy debate 
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367

Reply via email to