Regex brain fart!
Yesterday I got stuck for far too long on a simple regex not doing what I expected it to do. Here is an example:
sub prefix { return $1 if $_[0] =~ m/^(\w+)_/; return 'unknown'; } say prefix( 'radio_some_very_long_var_name' ); say prefix( 'comment_blahblah' );
I was “hoping” to see radio and comment but got back an incorrect radio_some_very_long_var on first item (and unfortunately I hadn’t noticed the _name had been dropped. That may have saved a lot of the heartache!).
I looked long and hard at the regex thinking…
what is wrong with \w+ ?
Of course what was wrong was me 😦
\w regex includes matching the _ (underscore) 🙂
From perlre pod
\w – Match a “word” character (alphanumeric plus “_”)
I don’t think I’ve ever needed the pleasure of knowing that… but I do now! Here’s the correct regex i needed:
m/^([A-Za-z]+)_/
Doh!
/I3az/
/^(\w+?)_/ should would too.
Yep it does Sam. Also
m/^(.*?)_/
does the biz.Grrr.. I hate regexes/regexen… especially late at night 🙂
/I3az/
Fell out of bed feeling down. This has britgenhed my day!
m/^([[:alpha:]]+)_/