Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am wanting to split the following string into 2 variables.
12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,btn car top T + J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L
My desired result would be
$string1 = "12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,btn car top T J Bau 57 (12) 7/7

$string2 = "Wnr: VENUS SERENA 1:26.82 13.5L"
How can I split each time on the key word "Wnr"
Thanks

Replies are listed 'Best First'.
Re: split string on key word
by sauoq (Abbot) on Sep 22, 2005 at 22:39 UTC
    my ($string1, $string2) = split /(?=Wnr:)/, $original;

    Update: (A little explanation at Moriarty's suggestion.) That's a zero-width positive look-ahead assertion. In other words, it splits the string right before "Wnr:" without losing any characters. I can't tell from the OP whether a trailing space should be kept on $string1. (The example doesn't include the ending double quote.) If you want to remove that space, you could put a literal space (or, better, \x20) before the opening paren. You could also use \s or \s+ there, depending on the behavior you need.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: split string on key word
by GrandFather (Saint) on Sep 22, 2005 at 22:40 UTC

    Use a regEx:

    my $str = '12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,bt +n car top T J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L'; my ($str1, $str2) = $str =~ /^(.*?)(Wnr:.*)/; print "$str1\n$str2\n";

    Perl is Huffman encoded by design.
Re: split string on key word
by ickyb0d (Monk) on Sep 22, 2005 at 22:40 UTC
    Might be a little shady, but should work.
    my @array_of_strings; my $string = '12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide +,btn car top T J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L'; @array_of_strings = split(/Wnr/, $string); #replacing removed Wnr $array_of_strings[1] = 'Wnr' . $array_of_strings[1]; # $array_of_strings[0] = '12-12 WANG 28 Jul H PRG SPEC 1320m Bk past +midfield wide,btn car top T J Bau 57 (12) 7/7' # $array_of_strings[1] = 'Wnr: VENUS SERENA 1:26.82 13.5L'
Re: split string on key word
by moot (Chaplain) on Sep 22, 2005 at 22:41 UTC
    my ($s1, $s2) = $string =~ /(.+)?(Wnr.+)/si;
Re: split string on key word
by VenROCK (Acolyte) on Sep 23, 2005 at 02:22 UTC
    or like this:

    $_='12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,btn car top T + J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L';
    /Wnr/;
    print $`;
Re: split string on key word
by VenROCK (Acolyte) on Sep 23, 2005 at 02:26 UTC
    #and the second part
    print $&.$';
      Quick 'n dirty:
      ($str1,$str2) = split(/Wnr:/, "some one 7/7 Wnr: some 2 2/3"); $str2 = "Wnr:".$str2; print "$str1\n$str2\n";
      some one 7/7
      Wnr: some 2 2/3
Re: split string on key word
by udyog (Acolyte) on Sep 23, 2005 at 14:07 UTC
    $var='12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,btn car + top T J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L'; ($string_1, $string_2)=$var=~m#(.+?)(Wnr.+?)$#; print "\n$str_1\n\n$str2";

    OUTPUT:

    12-12 WANG 28 Jul H PRG SPEC 1320m Bk past midfield wide,btn car top T + J Bau 57 (12) 7/7 Wnr: VENUS SERENA 1:26.82 13.5L