Perl 用“and”分割逗号分隔的列表

发布于 2024-11-19 18:28:23 字数 381 浏览 2 评论 0原文

使用Perl,我想在逗号和“and”上分割一个字符串(前面可能有也可能没有逗号。

  1. “Apple”给出数组< code>(Apple)
  2. "Apple and Orange" 给出数组 (Apple Orange)
  3. "Apple, Orange, and Banana" 给出array (Apple Orange Banana)

由于某种原因以下分割正则表达式对我不起作用:

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

Using Perl, I would like to split 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木森分化 2024-11-26 18:28:23

试试这个:

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 |, /

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 |, /
攀登最高峰 2024-11-26 18:28:23
my $str = 'Apple, Orange, and Banana';

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

$str =~ s/,?\s*and\b/,/;
my @words = split /\s*,\s*/, $str;
荒路情人 2024-11-26 18:28:23

以下 split 应该适合您:

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

The following split should work for you:

split /(?: ,?\s*and\s* | ,\s+? )/x;
可遇━不可求 2024-11-26 18:28:23

这是一种解决方案。它依赖于两个 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文