开发者

How do I extract data between square brackets that appear several times in a line using perl?

I have a line that contain开发者_Go百科es multiple instances of square bracketed data.

[data 1] junk [data 2] junk,junk [data 3] junk [data 4]

Does any one have a goo regex? So I can use

print $1,$2,$3,$4;

Thanks!


Use Text::Balanced instead of a regex.


my $s = "[data 1] junk [data 2] junk,junk [data 3] junk [data 4]";
my ($one, $two, $three, $four) = $s =~ /\[([^\]]*)\]/g;
print $one, $two, $three, $four;


If all your looking for is a quick printout, this should do it ..

$s = q( [data 1] junk [data 2] junk,junk [data 3] junk [data 4] );
print join(', ', @{[$s =~ /\[(.*?)\]/g]}), "\n";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜