Welcome to the Monastery! First lesson: put "<code>" and "</code>" tags around your perl snippets and data samples.

As for the question itself, I could suggest a regex that would be more likely to work:

$file = '_error' if ( $file =~ /(?:^compiled)|[^A-Za-z0-9]/ ); # updated: not "unless"
That demonstrates the two uses of the caret: as a start-of-string anchor and as an inversion operator within a character-class (i.e. match any character not specified within the sqaure brackets). BTW, if you can accept underscores along with digits and letters, use "\w" in place of "A-Za-z0-9_".

But all that doesn't explain why the negative look-behind didn't work the way you wanted. I'll have to think about that for a bit.

(update: as indicated in the comment line above, my initial post had the logic inverted. The OP said "set $file to _error if it starts with "compiled" or if it contains any character that is not alphanumeric. Since these are the things my regex tests for, the assignment must happen if there's a match, not the other way around. As for why the look-behind doesn't work, I think ikegami has it -- the entire OP regex is anchored at start-of-string, so there's no place to "look-behind".)


In reply to Re: Failed regex: negative look-behinds by graff
in thread Failed regex: negative look-behinds by loikiolki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.