Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to express contents of a file as regex metachars?

by jwkrahn (Monsignor)
on Jun 16, 2012 at 05:54 UTC ( #976532=note: print w/replies, xml ) Need Help??


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!