开发者

Using Look-ahead and Look-behind (perl)

I want to split with perl code this type开发者_运维知识库 of line:

my $my_town = Madrid(32): Villa de Madrid (ES) Royal Palace of Madrid (Teatro Real)

to:

Madrid(32):
Villa de Madrid (ES)
Royal Palace of Madrid (Teatro Real)

I tried:

my @travel = split(/(?<=\))\s*/, $my_town);

but output result is:

Madrid(32)
: Villa de Madrid (ES)
Royal Palace of Madrid (Teatro Real)

Please give me a little help.

Is there any way that function look-behind for ")" to return all finds fewer first character? ": Villa de Madrid (ES)" without first string ":", "Royal Palace of Madrid (Teatro Real)" without first string "white space", ...


split could do the job

my @matches = split(
   qr/
      (?: (?<= \) : ) 
      |   (?<= \) (?! : ) )
      )
      \s*
   /x,
   $my_town,
);

But since the input doesn't really look like a uniformly separated list to me, I think it's the wrong tool for the job. Notice how moving towards a parser is simpler:

my @matches = $my_town =~ / \S .*? \( [^)]+ \) :? /xgs;


Not a very scientific way to split up a line.

use strict;
use warnings;

my $my_town = 'Madrid(32): Villa de Madrid (ES) Royal Palace of Madrid (Teatro Real)  ';
my @travel = split( / (?<=\)):? \s* /x  , $my_town );

for (@travel) {
   print "'$_'\n";
}

'Madrid(32)'
'Villa de Madrid (ES)'
'Royal Palace of Madrid (Teatro Real)'

** edit **

I don't know why but if you're just wrestling with that variable length lookbehind issue
and want to see how you could include an optional colon : as part of the split
you could always do this:

my @travel = split( / (?:(?<=\):)|(?<=\))(?!:)) \s* /x  , $my_town );

'Madrid(32):'
'Villa de Madrid (ES)'
'Royal Palace of Madrid (Teatro Real)'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜