in reply to Split this string

my $string = <<HTML; this=x that=x x another=x thing=x HTML my @array = $string =~ /(?:^| )(.+?)(?=(?: \w+=|$))/g; print "[$_]\n" for @array; # whoops, was "[$_\n]" # prints: [this=x] [that=x x] [another=x] [thing=x]

Update: Corrected typo, per johngg below.

Replies are listed 'Best First'.
Re^2: Split this string
by johngg (Canon) on Oct 12, 2011 at 13:44 UTC

    I think your code would be more likely to print

    [this=x ][that=x x ][another=x ][thing=x ]

    Perhaps you meant

    print "[$_]\n" for @array;

    I hope this is helpful.

    Cheers,

    JohnGG

Re^2: Split this string
by Lotus1 (Vicar) on Oct 12, 2011 at 14:43 UTC

    I used \s* in place of (?:^| ) to make it work across blank lines.

    use strict; use warnings; my $string = <<HTML; this=x that=x x another=x thing=x van=x x x diesel=xx xx HTML my @array2 = $string =~ /\s*(.+?)(?=\s+\w+=|$)/g; print "[$_]\n" foreach (@array2);
Re^2: Split this string
by Anonymous Monk on Oct 12, 2011 at 14:14 UTC
    This helped me out, Thanks++.