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

Hi Monks!

My goal is to only split the string if its not a one word string or a filename, I can't have the match work when I have one word string.
Here is my sample code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); # Test cases: my $string_a = "Joe Dolly"; # It should split and print JD. my $string_b = "perl.pl"; # It should match on the regular expressio +n and nothing prints. my $string_c = "States"; # It should match on the regular expressio +n and nothing prints. my @got = $search_c =~ /^(?!\w)|(\.[^.]+)$/ig ? [] : split //, $string +_c; print Dumper $got[0]; print Dumper $got[1];
Thanks for looking!

Replies are listed 'Best First'.
Re: Split string only on regular expression match
by Athanasius (Archbishop) on Oct 31, 2017 at 16:23 UTC

    TMTOWTDI:

    use strict; use warnings; for ('Joe Dolly', 'perl.pl', 'States', 'Fred Fillmore Flintstone') { my @got = split; if (scalar @got > 1) { print substr($_, 0, 1) for @got; print "\n"; } }

    Output:

    2:16 >perl 1835_SoPW.pl JD FFF 2:17 >

    (If this doesn’t accomplish what you want, you will need to explain what you’re really trying to do with that regular expression.)

    Update: If you assign [] to an array, that array contains a single element, which is itself a reference to an empty array. I think you meant to rather assign () (the empty list), which leaves the array empty (zero elements).

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      You're right about assigning with () instead of [].
      The part I didn't explain is that the "Test cases" are one at a time and using regular expression seems to be the most straight forward way of doing it.
      All I am trying to say in the regexp is, to only SPLIT the string if it has two words, excluding one word and if its a filename( a word followed by a .(dot) ) and letters as extensions.
Re: Split string only on regular expression match
by hippo (Archbishop) on Oct 31, 2017 at 16:16 UTC

    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"; };
      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;