Perl用“和”分隔逗号分隔列表

时间:2011-07-12 12:55:10

标签: regex perl csv split

使用Perl,我想split逗号上的字符串和“和”(可能会或可能不会以逗号开头。

  1. "Apple"提供了数组(Apple)
  2. "Apple and Orange"提供了数组(Apple Orange)
  3. "Apple, Orange, and Banana"提供了数组(Apple Orange Banana)
  4. 出于某种原因,以下拆分正则表达式对我不起作用:

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

4 个答案:

答案 0 :(得分:3)

试试这个:

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

示例:

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

或者更简单的工作案例:

/,? and |, /

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

以下split适合您:

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

答案 3 :(得分:1)

这是一个解决方案。它依赖于两个split执行所有繁重的工作,为方便起见map。它可能与其他答案没有什么不同,但它很干净,而且眼睛相当容易(除了印刷品)。我认为它适用于空白/和/逗号的大多数变体。

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

输出:

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