in reply to splitting a string on pre-defined tags

#!/usr/bin/perl # http://perlmonks.org/?node_id=1215064 use strict; use warnings; use Data::Dumper; my $string='C*ID1*Mac*C release for EA\'s D*ID1*Spore1 game*D; D*ID1*S +pore 1*D is better than D*ID2*Spore 2 game*D.'; my @fields; push @fields, $& while $string =~ /\b([A-Z])\*.*?\*\1\b/g; print Dumper \@fields;

Outputs:

$VAR1 = [ 'C*ID1*Mac*C', 'D*ID1*Spore1 game*D', 'D*ID1*Spore 1*D', 'D*ID2*Spore 2 game*D' ];

Replies are listed 'Best First'.
Re^2: splitting a string on pre-defined tags
by jimpudar (Pilgrim) on May 22, 2018 at 20:43 UTC

    You beat me to it!

    use strict; use warnings; use 5.10.0; my $string="C*ID1*Mac*C release for EA's D*ID1*Spore1 game*D; D*ID1*Sp +ore 1*D is better than D*ID2*Spore 2 game*D."; my @fields; while ($string =~ /([A-Z])\* # A single capital letter followed by s +tar ID\d+\* # String 'ID' followed by a number and +a star .*? # Anything \*\1 # A star followed by the original singl +e capital letter /gx) { push @fields, $&; }

    Jim