如何在 Perl 中从逗号分隔的字符串中提取单词?
我有一行:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不使用
split
函数:split< /code> 使用您提供的正则表达式作为分隔符将字符串拆分为字符串列表。
Why not use the
split
function:split
splits a string into a list of strings using the regular expression you supply as a separator.使用 my @parts = split(/,/, $myline) 不是更容易吗?
Isn't it easier to use
my @parts = split(/,/, $myline)
?尽管 split 是解决问题的好方法,但在列表上下文中捕获正则表达式也能很好地工作。了解这两种方法很有用。
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.查看可以从 CPAN 下载的 CSV PM,即
Text::CSV
或Text::CSV_XS
。这将为您提供所需的内容,并考虑到任何恰好被引用的逗号分隔值。
使用这些模块可以轻松地拆分数据并对其进行解析...
例如:
Look into the CSV PM's you can download from CPAN, i.e.
Text::CSV
orText::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:
如果元素的数量是可变的,那么您将不会按照您的目标方式进行操作。使用全局标志循环遍历字符串:
我猜测您的真实数据比“ca,cb,cc,cd,ce”更复杂,但是如果不是,那么使用正则表达式可能不是保证。您最好在分隔字符上拆分字符串:
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:
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: