如何在 Perl 中从逗号分隔的字符串中提取单词?

发布于 2024-08-13 09:08:27 字数 333 浏览 3 评论 0原文

我有一行:

$myline = 'ca,cb,cc,cd,ce';

我需要将 ca 匹配到 $1,将 cb 匹配到 $2,等等。

不幸的

$myline =~ /(?:(\w+),?)+/;

是没有不工作。对于 pcretest,它仅将 'ce' 匹配到 $1。 怎样做才是正确的?
我需要将其放入 while 循环中吗?

I have a line:

$myline = 'ca,cb,cc,cd,ce';

I need to match ca into $1, cb into $2, etc..

Unfortunately

$myline =~ /(?:(\w+),?)+/;

doesn't work. With pcretest it only matches 'ce' into $1.
How to do it right?
Do I need to put it into the while loop?

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

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

发布评论

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

评论(5

枕梦 2024-08-20 09:08:27

为什么不使用 split 函数

@parts = split(/,/,$myline);

split< /code> 使用您提供的正则表达式作为分隔符将字符串拆分为字符串列表。

Why not use the split function:

@parts = split(/,/,$myline);

split splits a string into a list of strings using the regular expression you supply as a separator.

眉黛浅 2024-08-20 09:08:27

使用 my @parts = split(/,/, $myline) 不是更容易吗?

Isn't it easier to use my @parts = split(/,/, $myline) ?

预谋 2024-08-20 09:08:27

尽管 split 是解决问题的好方法,但在列表上下文中捕获正则表达式也能很好地工作。了解这两种方法很有用。

my $line = 'ca,cb,cc,cd,ce';
my @words = $line =~ /(\w+)/g;

Although split is a good way to solve your problem, a capturing regex in list context also works well. It's useful to know about both approaches.

my $line = 'ca,cb,cc,cd,ce';
my @words = $line =~ /(\w+)/g;
日裸衫吸 2024-08-20 09:08:27

查看可以从 CPAN 下载的 CSV PM,即 Text::CSVText::CSV_XS

这将为您提供所需的内容,并考虑到任何恰好被引用的逗号分隔值。

使用这些模块可以轻松地拆分数据并对其进行解析...

例如:

my @field = $csv->fields;

Look into the CSV PM's you can download from CPAN, i.e. Text::CSV or Text::CSV_XS.

This will get you what you need and also account for any comma seperated values that happen to be quoted.

Using these modules make it easy to split the data out and parse through it...

For example:

my @field = $csv->fields;
彻夜缠绵 2024-08-20 09:08:27

如果元素的数量是可变的,那么您将不会按照您的目标方式进行操作。使用全局标志循环遍历字符串:

while($myline =~ /(\w+)\b/g) {
    # do something with $1
}

我猜测您的真实数据比“ca,cb,cc,cd,ce”更复杂,但是如果不是,那么使用正则表达式可能不是保证。您最好在分隔字符上拆分字符串:

my @things = split ',', $myline;

If the number of elements is variable, then you're not going to do it in the way you're aiming for. Loop through the string using the global flag:

while($myline =~ /(\w+)\b/g) {
    # do something with $1
}

I am going to guess that your real data is more complex than 'ca,cb,cc,cd,ce', however if it isn't then the use of regular expressions probably isn't warranted. You'd be better off splitting the string on the delimiting character:

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