大概可以实现,要用到
look-around match , 或者用split 分隔开也行
#!/usr/bin/perl
use strict ;
use warnings ;
use Data::Dumper ;
$_ = qq~
[img]aa[/img]
[img]bb[/img] [img]bb[/img]
文字
[img]cc[/img] [img]cc[/img] [img]cc[/img]
ddddd
[img]cc[/img] [img]cc[/img] [img]cc[/img] [img]cc[/img]
~;
# use split maybe more clear : depends on you
#my @data = grep { length } split /(\s*\[img\]\S+\[\/img\]\s*)/ , $_ ;
#print Dumper \...@data ;
# method 1 : keep the last one :
s/ \[img\] \S+ \[\/img\] (?= \s* \[img\] \S+ \[\/img\] \s* ) //xg ;
print ;
#or
# method 2 : keep the first one : divide into 3 substeps : can control
# more detailedly ,
# step1 : front has not , back has
s/ (?<!\[\/img\]) \s* (\[img\]\S+\[\/img\]) \s* (?=\[img\]) /$1/xg ;
# step2 : front /back both has
s/ (?<=\[\/img\]) \s* (\[img\]\S+\[\/img\]) \s* (?=\[img\]) //xg ;
# step3 : front has , back has not
s/ (?<=\[\/img\]) \s* (\[img\]\S+\[\/img\]) \s* (?!\[img\]) //xg ;
print ;
2009/9/5 imxae <[email protected]>
> 就是合并连html标签的问题,如:
>
> str=qq~
> [img]aa[/img]
> [img]bb[/img]
> 文字
> [img]cc[img]
> ~;
>
> 处理之后:
>
> str=
> [img]aa[/img]
> 文字
> [img]cc[/img]
> ;
>
>
> 因为上面[img]标签aa与bb是连续的,中间只有空格或回车,也算是连续的.所以处理后应剩一个.后同的cc因为前面有文字内容和前面的标签分隔开了,而且只有一个就保留下来.
>
> 就是处理因为连续重复出现的标签.不知道说清楚了没有..
>
>
> 2009/9/5 Michael Zeng <[email protected]>:
> > 没看懂问题,能说得更清除写么
> >
> >
> > 2009/9/5 蓝天下云层上 <[email protected]>
> >>
> >> $str=qq~[img]sss[/img]
> >> [img]bbb[/img][img]ccc[/img]~;
> >>
> >> $str=~ ..;
> >>
> >>
> >> 如何把上面连续标签[img]合并成一个[img]sss[/img],只要有两个或两个以上一样的标签出现只保留一个?
> >
> >
> >
> > --
> > Yours Sincerely
> > Zeng Hong
> >
> > >
> >
>
> >
>
--
Yours Sincerely
Zeng Hong
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
要在此论坛发帖,请发电子邮件到 [email protected]
要退订此论坛,请发邮件至 [email protected]
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---