请教关于一个split的问题

请教关于一个split的问题

请教关于一个split的问题
1 1:

#! /usr/bin/perl -w

my $a = "<i am file>you are good<heihei>";

my @b = split /[<>]/, $a;

foreach (0..$#b)
{
print "$b[$_]\n";
}
结果:


i am file
you are good
heihei



2

#! /usr/bin/perl -w

my $a = "<i am file>you are good<heihei>";

my @b = split /([<>])/, $a;

foreach (0..$#b)
{
print "$b[$_]\n";
}
结果:


<
i am file
>
you are good
<
heihei
>

但这两种结果都不是我想要的,我想要的结果是:
<i am file>
you are good
<heihei>

请教大家要这种结果该怎么写split[color=#0000FF][/color][color=ff00ff][/color][color=red][/color][color=red][/color]




   

干吗非要用split用替换貌似会更简单
[code]
my $a = "<i am file>you are good<heihei>";
$a=~s/</\n</g;
$a=~s/>/>\n/g;
print $a;
[/code]
你的要求似乎用 split 无法完成,为什么不用正则?
#! /usr/bin/perl -w--<.
#! /usr/bin/perl -w

my $a = "<i am file>you are good<heihei>";
my @b = $a =~/(<.*?>)(.*?)(<.*?>)/;

foreach (0..$#b)
{
print "\$b[$_] = $b[$_]\n";
}
稍微改了下队长的----.
稍微改了下队长的

my $a = "<i am file>you are good<heihei><><!>";
my @b = $a =~/<.*?>|[^<>]+/g;

运行结果:
$b[0] = <i am file>
$b[1] = you are good
$b[2] = <heihei>
$b[3] = <>
$b[4] = <!>


PS: 最近的贴不少是拆分的, 比如CSV, "", 还带嵌套... 可学习下.




   

thanks everyone.