Durai <[EMAIL PROTECTED]> wrote:
: 
: The following scripts works fine.
:
: But I have doubt in this.

    Why do you doubt this?

    Your script defines $_, looks for a match in $_,
then ignores the captured parts of the match and
prints $_.


: $_="The quick black fox slavered over the dead dog   # a bit 
: macabre, no?";
: 
: $_ =~ m/^                         # anchor at beginning of line
:      The\ quick\ (\w+)\ fox    # fox adjective
:      \ (\w+)\ over             # fox action verb
:      \ the\ (\w+) dog          # dog adjective
:      (?:                       # whitespace-trimmed comment:
:        \s* \# \s*              #   whitespace and comment token
:        (.*?)                   #   captured comment text; non-greedy!
:        \s*                     #   any trailing whitespace
:      )?                        # this is all optional
:      $                         # end of line anchor
:     /x;                        # allow whitespace
: print;
: 
: I just modified the following line
: 
:  (?:                       # whitespace-trimmed comment:
: 
:                 to
: (?                       # whitespace-trimmed comment:
: 
: I got error. Why?

    Because you removed the colon ':'.


    BTW, the regex is failing to match, though the script is
not testing for that. If you were to test for a successful
match the script might look like this.


use strict;
use warnings;

$_="The quick black fox slavered over the dead dog   # a bit macabre, no?";

print qq("$1", "$2", "$3", "$4"\n) if
    $_ =~ m/^                   # anchor at beginning of line
        The\ quick\ (\w+)\ fox  # fox adjective
        \ (\w+)\ over           # fox action verb
        \ the\ (\w+) dog        # dog adjective
        (?:                     # white space-trimmed comment:
            \s* \# \s*          #   white space and comment token
            (.*?)               #   captured comment text
            \s*                 #   any trailing white space
        )                       # this is all optional
        $                       # end of line anchor
        /x;                     # allow white space

__END__

    Which doesn't print anything because of the failed match.
The error can be found on the fourth line of the regular
expression.


        \ the\ (\w+) dog        # dog adjective

    which should be:

        \ the\ (\w+)\ dog       # dog adjective



HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


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


Reply via email to