[英]How do I extract single characters or enclosed groupings from a string in Perl?
I would like to split the string: "Hello[You]All" 我想拆分字符串:“Hello [You] All”
to the following array: 到以下数组:
H,e,l,l,o,[You],A,l,l
I tried to do it with split: 我尝试用拆分做到这一点:
my $str = "Hello[You]All";
my @list = split(/(\[.*?\]|.)/, $str);
foreach (@list) {
print "->$_\n";
}
Since I tried something that split is not supposed to do, it gave me the following array: 既然我尝试了分裂不应该做的事情,它给了我以下数组:
,H,,e,,l,,l,,o,,[You],,A,,l,,l,
Next step I need to take is to remove the empty spaces. 我需要采取的下一步是删除空格。
While it is not the best solution it is the only one I found, without anything too messy. 虽然它不是最好的解决方案,但它是我找到的唯一解决方案,没有任何太混乱。 I'm posting here to ask if anyone knows a better way to solve this task? 我在这里发帖询问是否有人知道更好的方法来解决这个问题?
my $str = "Hello[You]All";
my @list = $str =~ /(\[.*?\]|.)/g;
foreach (@list) {
print "->$_\n";
}
Which is to say: you don't need to split on the pattern you're using (which causes those empty elements, because they're the actual text that's been split out using your pattern as a divider); 也就是说:你不需要拆分你正在使用的模式(导致那些空元素,因为它们是使用你的模式作为分隔符拆分出来的实际文本); you just need to extract all matches for your pattern. 你只需要为你的模式提取所有匹配项。 Which doing a global ( /g
) pattern match in array context does. 哪个在数组上下文中进行全局( /g
)模式匹配呢。
You could grep the results for non-empty elements; 你可以grep非空元素的结果;
my @list = grep /./, split(/(\[.*?\]|.)/, $str);
Alternatively, 或者,
my @list = $str =~ /\[.*?\]|./g;
While I also think chaos' answer is the right one here, for completeness, here is one way of achieving what you want using split
and grep
: 虽然我也认为混乱的答案在这里是正确的,但为了完整性,这里是使用split
和grep
实现你想要的一种方法:
#!/usr/bin/perl
use strict;
use warnings;
my $x = "Hello[You]All";
my @x = grep { defined } split qr{(\[.+\])|}, $x;
use Data::Dumper;
print Dumper \@x;
Using this pattern, split
splits either on characters within brackets (you did not mention if "a[]b"
is a valid input) or the empty string and the grep
filters on defined
ness rather than truth value. 使用此模式, split
括号内的字符(您没有提及"a[]b"
是否为有效输入)或空字符串和grep
过滤器defined
ness而不是真值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.