in reply to Split string only on regular expression match

Is your test actually just a test for whitespace?

#!/usr/bin/perl use strict; use warnings; # Test cases: my @strings = ("Joe Dolly", # It should split and print JD. "perl.pl", # It should match on the regular expression and nothi +ng prints. "States"); # It should match on the regular expression and noth +ing prints. for my $str (@strings) { next unless $str =~ /\s/; my $out = join '', map { $_ = substr ($_, 0, 1) } split (/\s/, $st +r); print "$out\n"; };

Replies are listed 'Best First'.
Re^2: Split string only on regular expression match
by Anonymous Monk on Oct 31, 2017 at 17:17 UTC
    As I explained above, I can't use an array, the search string comes one at a time, I have a typo on this line:
    my @got = $search_c =~ /^(?!\w)|(\.[^.]+)$/ig ? [] : split //, $strin +g +_c;
    It should be:
    my @got = $string_c =~ /^(?!\w)|(\.[^.]+)$/ig ? [] : split //, $string +_c;
    Thanks!
      I used a simpler regular expression to do what I want as:
      my @got = $string_c !~ /^\w+\s+\w+$/ig ? () : split /\s/, $string_c;