in reply to Lexing: how to define tokens based on "context"

Hello three18ti,

Since the rule name can be any alphanumeric string including _ a simple "\w+" won't suffice.

I should think something like  [\w\_]+ would work:

#!/usr/bin/perl use strict; use warnings; my $testval = "KUNG_FOO=bar"; if ($testval =~ /([\w\_]+)/) { my $keyname = $1; print "KEY = [$keyname]\n"; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-10-16@0800-Underscore>testregex.pl KEY = [KUNG_FOO]

Replies are listed 'Best First'.
Re^2: Lexing: how to define tokens based on "context"
by dave_the_m (Monsignor) on Oct 16, 2013 at 14:38 UTC
    Since the rule name can be any alphanumeric string including _ a simple "\w+" won't suffice.

    I should think something like [\w\_]+ would work:

    \w already matches underscore.

    Dave.

      D'oh!

      /meslinks off to the closest dark corner to try to hide.

      :-)