开发者

Perl split comma separated list with "and"

开发者 https://www.devze.com 2023-03-19 11:25 出处:网络
Using Perl, I would like to s开发者_StackOverflow中文版plit a string on the comma and the \"and\" (that may or may not be preceded by a comma.

Using Perl, I would like to s开发者_StackOverflow中文版plit a string on the comma and the "and" (that may or may not be preceded by a comma.

  1. "Apple" gives array (Apple)
  2. "Apple and Orange" gives array (Apple Orange)
  3. "Apple, Orange, and Banana" gives array (Apple Orange Banana)

For some reason the following split regex is not working for me:

split(/(,| and )/, $string)


Try this:

my @list = split /\s*(?:(?:,\s*)?\band\b|,)\s*/, $string;

Example:

perl -E "say join ':', split /\s*(?:(?:,\s*)?\band\b|,)\s*/, 'apple, orange, and banana'"
apple:orange:banana

Or a simpler working case for your example:

/,? and |, /


my $str = 'Apple, Orange, and Banana';

$str =~ s/,?\s*and\b/,/;
my @words = split /\s*,\s*/, $str;


The following split should work for you:

split /(?: ,?\s*and\s* | ,\s+? )/x;


Here's one solution. It relies on two split doing all the heavy lifting, with a map for convenience. It's probably not very different from the other answers, but it's clean, and it's fairly easy on the eyes (except maybe for the print). And I think it will work with most variations on whitespace/and/comma.

use warnings;
use strict;

my @w = ( "Apple", "Apple and Orange", "Apple, Orange, and Banana",
    "Apple, Orange and Banana ,and Pineapple" );

for (@w) {
    print join (':', humanize($_)), "\n";
}


sub humanize {
    my $str = shift;
    my @list = split /\s*and\s*/, $str;
    @list = map { split /\s*,\s*/, $_ } @list;
    return @list;
}

Output:

Apple
Apple:Orange
Apple:Orange:Banana
Apple:Orange:Banana:Pineapple
0

精彩评论

暂无评论...
验证码 换一张
取 消