in reply to Regex pattern that fails when string contains a certain pattern

#!/usr/bin/perl # http://perlmonks.org/?node_id=1178340 use strict; use warnings; while(<DATA>) { print /^(?!.*_\d)/ ? " matched $_" : "not matched $_"; } __DATA__ aa1.2 aa1_bb2 aa1_2 aa1_2bb 1aa

Your last test case '1aa' has no underscore and therefor by your rule should match.

Edit after your edit (I think)

/^(?!.*[-_]\d)/

Replies are listed 'Best First'.
Re^2: Regex pattern that fails when string contains a certain pattern
by hoppfrosch (Scribe) on Dec 22, 2016 at 09:47 UTC
    Thanks!!!
    You're right: my specification was incomplete.
    Extended specification: Strings starting with a number should not be matched as well
      #!/usr/bin/perl # http://perlmonks.org/?node_id=1178340 use strict; use warnings; while(<DATA>) { print /^(?!.*(?:-|_|^)\d)/ ? " matched $_" : "not matched $_"; } __DATA__ aa1.2 aa1_bb2 aa1_2 aa1_2bb 1aa