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

Here's how I would have presented this question, with an SSCCE:

use strict; use warnings; use Test::More; my @good = ( 'aa1.2', 'aa1_bb2' ); my @bad = ( 'aa1_2', 'aa1_2bb', '1aa' ); my $re = qr/^[a-z][^_]*(_[a-z].*)?$/; plan tests => @good + @bad; for my $str (@good) { like ($str, $re, "$str matched"); } for my $str (@bad) { unlike ($str, $re, "$str not matched"); }

Note that this is a working solution as well as a statement of the problem. See also How to ask better questions using Test::More and sample data.

  • Comment on Re: Regex pattern that fails when string contains a certain pattern
  • Download Code