Perl regular expression question
I would like to use Perl split function on comma delimited lines and capture onl开发者_开发技巧y two or more words between the comma delimited into an array. Single words are not needed.
For example, this line -->> aaa, ccc ddd, eee, fff ggg uuu,
I only want, ccc ddd and fff ggg uuu
while(<FH>)
{
@ = split(/,/);
}
demo script:
my @data = (
'aaa, ccc ddd, eee, fff ggg uuu'
, ' aaa bbb ,ccc,eee,fff ggg uuu '
, 'aaa,ccc,eee,fff'
);
for my $line (@data) {
printf "|%s| ==> \n", $line;
$line =~ s/^\s+|\s+$//g;
my @cut = grep { / / } split( /\s*,\s*/, $line );
printf "|%s|\n\n", join( '|', @cut );
}
output:
|aaa, ccc ddd, eee, fff ggg uuu| ==>
|ccc ddd|fff ggg uuu|
| aaa bbb ,ccc,eee,fff ggg uuu | ==>
|aaa bbb|fff ggg uuu|
|aaa,ccc,eee,fff| ==>
||
less terse than Dallaylaen's solution, but may make it easy to check for/handle special cases.
UPDATE: Added " aaa," protection, and ", ," protection as well.
$line =~ s/^\s+|\s+$//g; # or you get false positives
my @multiword = grep {/\s/} split /\s*,[,\s]*/, $line;
The split will eat up all space around commas, so anything in the array that contains a space is a multi-word.
It's as simple as:
(undef, undef, @parsedvalues) = split /,/
You esentialy throw away first two values you get from split.
精彩评论