Dinesh Ravi Dinesh Ravi - 12 days ago 6
Perl Question

Perl: How to pull out the desired string using the starting character but needs to include them as well?

I have this string

my $word = "Chase_^%798(987%55,.#*&^*&Chase_$&^**&(()%%hjjlhh";


Desired output is

Chase_^%798(987%55,.#*&^*&
Chase_$&^**&(()%%hjjlhh


The string
"Chase_"
is the only clue using which I should separate them. Using split I lose the string
"Chase_"
. Then I should concatenate them. I don't have the slightest idea of how to split it but also the string
"Chase_"
should be present.`

Answer

Use lookahead:

my $str = 'Chase_^%798(987%55,.#*&^*&Chase_$&^**&(()%%hjjlhh';
my @list = split(/(?=Chase_)/, $str);
say Dumper\@list;

Output:

$VAR1 = [
          'Chase_^%798(987%55,.#*&^*&',
          'Chase_$&^**&(()%%hjjlhh'
        ];
Comments