tags:

views:

98

answers:

2

I want match

 my @array = ( 'Tree' , 'JoeTree');

    foreach (@array ) {
      if ( $_ =~ /^(Joe)Tree/gi) {
        print "matched $_";
      }
    }

It matching only JoeTree. It's not matching Tree ?

+5  A: 

You missed a ?: /^(Joe)?Tree/gi

splash
+10  A: 

Try:

if (/^(?:Joe)?Tree/gi)
  • We've made the Joe part optional.
  • Also you can change (..) to (?:...) as you are just grouping.
  • Also $_ =~ part is redundant as by default we check in $_
codaddict