Perl regex field separators
I have a small problem with one of my regular expressions. My data fields are set up as follows:
<3 spaces>my first column<3 spaces>my second column<3 spaces>etc.
My regular expression is
\s\s\s(.*?)\s\s\s
The issue I'm coming across is that the regex only matches every other column. That makes sense if regex is starting to be applied at the very end of the matching pattern not after the group definition - there is no set of three spaces for it to match at that point until the second column is reached.
How can I make this happen? My google-f开发者_如何学JAVAu is failing me.
you should use look-around assertions in regexp
/(?<=\s\s\s)(.*?)(?=\s\s\s)/
Maybe split can help:
use warnings;
use strict;
use Data::Dumper;
my $str = ' my first column my second column etc.';
my @cols = split /\s{3}/, $str;
print Dumper(\@cols);
__END__
$VAR1 = [
'',
'my first column',
'my second column',
'etc.'
];
Although, it does create a leading element.
Shouldn't you use split(/\s\s\s/, $line)
?
精彩评论