in reply to An efficient way to gather a common portion of several strings' beginnings

Using capture groups, only print (or push or add to hash) values that match.
use strict; use warnings; my @strings = ( 'string that I need to gather the common base from: number 1 and some +other junk in it', 'string that I need to gather the common base from: number 2 and some +other junk in it', 'string that I need to gather the common base from: number 3 and some +other junk in it', 'string that doesnt meet the proper specifications: number 4 and some +other junk in it', 'another string that will fail to match: because it doesnt match DUH!' +, 'this string will fail as well' ); for(@strings){ print $1 . $2 . "\n" if (/^(string that I need to gather the commo +n base from:\s)(.*)/); }
  • Comment on Re: An efficient way to gather a common portion of several strings' beginnings
  • Download Code