开发者

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

开发者 https://www.devze.com 2023-02-04 01:32 出处:网络
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]

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

精彩评论

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

关注公众号