Skip to content

Regex brain fart!

March 11, 2010
tags: ,

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/

Advertisement
4 Comments leave one →
  1. March 11, 2010 9:44 pm

    /^(\w+?)_/ should would too.

    • March 11, 2010 10:15 pm

      Yep it does Sam. Also m/^(.*?)_/ does the biz.

      Grrr.. I hate regexes/regexen… especially late at night 🙂

      /I3az/

      • June 16, 2011 3:40 am

        Fell out of bed feeling down. This has britgenhed my day!

  2. Jay permalink
    March 12, 2010 9:26 pm

    m/^([[:alpha:]]+)_/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: