如何在perl中分割具有多个模式的字符串?

发布于 2024-12-18 04:25:04 字数 294 浏览 0 评论 0原文

我想用多种模式分割一个字符串:

例如。

my $string= "10:10:10, 12/1/2011";

my @string = split(/firstpattern/secondpattern/thirdpattern/, $string);

foreach(@string) {
    print "$_\n";
}

我想要的输出是:

10
10
10
12
 1
2011

执行此操作的正确方法是什么?

I want to split a string with multiple patterns:

ex.

my $string= "10:10:10, 12/1/2011";

my @string = split(/firstpattern/secondpattern/thirdpattern/, $string);

foreach(@string) {
    print "$_\n";
}

I want to have an output of:

10
10
10
12
 1
2011

What is the proper way to do this?

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

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

发布评论

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

评论(7

醉梦枕江山 2024-12-25 04:25:04

在正则表达式分隔符中使用 字符类 来匹配一组可能的字符分隔符。

my $string= "10:10:10, 12/1/2011";
my @string = split /[:,\s\/]+/, $string;

foreach(@string) {
    print "$_\n";
}

说明

  • 斜杠对/.../表示要匹配的正则表达式或模式。

  • 一对方括号[...]表示正则表达式的字符类。

  • 里面是可以匹配的可能字符集:冒号:、逗号,、任何类型的空格字符\s、和正斜杠 \/ (使用反斜杠作为转义字符)。

  • 需要 + 来匹配其前面的 1 个或多个字符,在本例中是整个字符类。如果没有这个,逗号空格将被视为 2 个单独的分隔符,从而在结果中为您提供一个额外的空字符串。

Use a character class in the regex delimiter to match on a set of possible delimiters.

my $string= "10:10:10, 12/1/2011";
my @string = split /[:,\s\/]+/, $string;

foreach(@string) {
    print "$_\n";
}

Explanation

  • The pair of slashes /.../ denotes the regular expression or pattern to be matched.

  • The pair of square brackets [...] denotes the character class of the regex.

  • Inside is the set of possible characters that can be matched: colons :, commas ,, any type of space character \s, and forward slashes \/ (with the backslash as an escape character).

  • The + is needed to match on 1 or more of the character immediately preceding it, which is the entire character class in this case. Without this, the comma-space would be considered as 2 separate delimiters, giving you an additional empty string in the result.

只怪假的太真实 2024-12-25 04:25:04

错误的工具!

my $string = "10:10:10, 12/1/2011";
my @fields = $string =~ /([0-9]+)/g;

Wrong tool!

my $string = "10:10:10, 12/1/2011";
my @fields = $string =~ /([0-9]+)/g;
金橙橙 2024-12-25 04:25:04

您可以按非数字进行拆分;

#!/usr/bin/perl
use strict;
use warnings;
use 5.014;

my $string= "10:10:10, 12/1/2011";
say for split /\D+/, $string;

You can split on non-digits;

#!/usr/bin/perl
use strict;
use warnings;
use 5.014;

my $string= "10:10:10, 12/1/2011";
say for split /\D+/, $string;
画骨成沙 2024-12-25 04:25:04
my $string= "10:10:10, 12/1/2011";

my @string = split(m[(?:firstpattern|secondpattern|thirdpattern)+], $string);

my @string = split(m[(?:/| |,|:)+], $string);

print join "\n", @string;
my $string= "10:10:10, 12/1/2011";

my @string = split(m[(?:firstpattern|secondpattern|thirdpattern)+], $string);

my @string = split(m[(?:/| |,|:)+], $string);

print join "\n", @string;
就像说晚安 2024-12-25 04:25:04

回答你原来的问题:
您正在寻找|运算符

my $string = "10:10:10, 12/1/2011";

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

foreach(@string) {
    print "$_\n";
}

但是,正如其他答案所指出的那样,您通常可以通过进一步简化或概括来改进这一点。

To answer your original question:
you were looking for the | operator:

my $string = "10:10:10, 12/1/2011";

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

foreach(@string) {
    print "$_\n";
}

But, as the other answers point out, you can often improve on that with further simplifications or generalizations.

扭转时空 2024-12-25 04:25:04

如果数字是您想要的,请提取数字:

my @numbers = $string =~ /\d+/g;
say for @numbers;

不需要捕获括号,如

/g 修饰符指定全局模式匹配——即匹配
在字符串中尽可能多地出现。它的行为方式取决于
上下文。在列表上下文中,它返回子字符串的列表
与正则表达式中的任何捕获括号匹配。 如果
没有括号,它返回所有匹配的列表
字符串,就好像整个模式周围都有括号。

If numbers are what you want, extract numbers:

my @numbers = $string =~ /\d+/g;
say for @numbers;

Capturing parentheses are not required, as specified in perlop:

The /g modifier specifies global pattern matching--that is, matching
as many times as possible within the string. How it behaves depends on
the context. In list context, it returns a list of the substrings
matched by any capturing parentheses in the regular expression. If
there are no parentheses, it returns a list of all the matched
strings, as if there were parentheses around the whole pattern.

我一向站在原地 2024-12-25 04:25:04

当您解析明显是日期/时间的内容时,我想知道使用 DateTime::Format::Strptime 将其解析为 DateTime 对象。

As you're parsing something that is rather obviously a date/time, I wonder if it would make more sense to use DateTime::Format::Strptime to parse it into a DateTime object.

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