Tweet

How do I split a string and keep the characters I split on?

For example, you have the following string:

    12.00 Play School 12.40 The Wiggles 13.00 Movie: The Wizard of Oz

And you want to split it into an array, where the elements of the array would be as follows:

    12.00 Play School
    12.40 The Wiggles
    13.00 Movie: The Wizard of Oz

If you just split on the times, you would lose them:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $text = "12.00 Play School 12.40 The Wiggles 13.00 Movie: The Wizard of Oz";

    my @data = split(/\d{2}\.\d{2}/, $text);

    foreach my $d (@data) {
        print "$d\n";
    }

    exit 0;

The produces the following output:

     Play School
     The Wiggles
     Movie: The Wizard of Oz

The Solution

Tell the split function to keep the matched characters. This solution is identical to the problem code above, except that the brackets in the match pattern regular expression tell split to keep the match data:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $text = "12.00 Play School 12.40 The Wiggles 13.00 Movie: The Wizard of Oz";

    my @data = split(/(\d{2}\.\d{2})/, $text);

    foreach my $d (@data) {
        print "$d\n";
    }

    exit 0;

This produces the following output:

    12.00
     Play School
    12.40
     The Wiggles
    13.00
     Movie: The Wizard of Oz

See also

    perldoc -f split
Revision: 1.5 [Top]