http://www.perlmonks.org?node_id=976532


in reply to How to express contents of a file as regex metachars?

if ($data =~ /^([\d\w]+)/ ) { notate('A', length($1)); }

Your use of [\d\w] is redundant because the \w character class also includes the same characters of the \d character class.    Also, the \w character class includes the _ character (underscore) which is not an alphanumeric character.    You should just use [A-Za-z0-9] which only matches alphanumeric characters.

You don't have to capture the match and get its length, you can just pass its length directly:

if ($data =~ /^[A-Za-z0-9]+/ ) { notate('A', $+[0]); }

Replies are listed 'Best First'.
Re^2: How to express contents of a file as regex metachars?
by dwhite20899 (Friar) on Jun 16, 2012 at 19:49 UTC
    Argh - I'm an idiot, I should have known \w wasn't correct. And I didn't know (or completely forgot) about $+ so thanks for that tip!