For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > Newbie: regular express









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie: regular express
Durai

2004-05-15, 6:30 am

Hello All,

The following scripts works fine. But I have doubt in this.

$_="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?

Regs,Durai.




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/24/2004
Charles K. Clarkson

2004-05-15, 8:30 am

Durai <tech_durai@yahoo.com> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com