in reply to Failed regex: negative look-behinds
As for the question itself, I could suggest a regex that would be more likely to work:
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_".$file = '_error' if ( $file =~ /(?:^compiled)|[^A-Za-z0-9]/ ); # updated: not "unless"
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".)
|
|---|