Re: Odd behaviour with Regular Expressions
[email protected] (David Manura) Thu, 04 Mar 2004 19:22:01 -0500
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
Paul,
Consider the following example:
===============================
use Parse::RecDescent;
my $grammar = <<'TEXT';
ident : /^(?!(return|exit|if))[A-Za-z0-9_]*/
{
print "pattern matched OK\n";
print $item[1];
}
TEXT
my $p = new Parse::RecDescent($grammar);
$p->ident('test if');
================================
This outputs
================================
pattern matched OK
test
================================
as you expect, so you're on the right track. Perhaps the '*' is causing ident
to match the empty string (''). For example, this
$p->ident('.test if');
outputs
=================================
pattern matched OK
=================================
-davidm
PerlDiscuss - Perl Newsgroups and mailing lists wrote:
> I am attempting to create a rule that will match any string apart from
> those that are keywords in my language.
>
> I have developed the following regular expression, which acheives this,
> but, when trying to access a word that is accepted by the regular
> expression, via $item[1], nothing is returned ( I have also tried
> $item[0], $item[2] etc). I have placed a print statement in as a debugging
> aid, to prove that the rule works properly debugging aid:
>
> When entering a keyword, such as "return", as expected, nothing is
> printed. However, when entering a non-keyword, such as "foo"
> only the string "pattern matched OK" is printed, and the value of the
> matched word ($item[1]) is not printed.
>
> Can anyone tell me where I am going wrong here?
>
> Followin is the offending piece of code
>
> Paul Kennerley
>
> ident : /^(?!(return|exit|if))[A-Za-z0-9_]*/
> {
> print "pattern matched OK\n";
> print $item[1];
> }
>
>