On 3 Sep 2007 at 16:12, Andrew Curry wrote:
> $ perl -le'
> $_ = q[SPEED OF LIGHT, , LIGHT SPEED,TRAVEL,TRAVELLING, ,
> DANGER,DANGEROUS,PHYSICAL, , CONCEPT,CONCEPTS, , , , , , , , , , ]; >
> print; s/,\s*(?=,)//g; print; '
> SPEED OF LIGHT, , LIGHT SPEED,TRAVEL,TRAVELLING, ,
> DANGER,DANGEROUS,PHYSICAL, , CONCEPT,CONCEPTS, , , , , , , , , , SPEED OF
> LIGHT, LIGHT SPEED,TRAVEL,TRAVELLING, DANGER,DANGEROUS,PHYSICAL,
> CONCEPT,CONCEPTS,
>
>
> $ perl -le'
> $_ = q[SPEED OF LIGHT, , LIGHT SPEED,TRAVEL,TRAVELLING, ,
> DANGER,DANGEROUS,PHYSICAL, , CONCEPT,CONCEPTS, , , , , , , , , , ]; print;
> $_ = join ",", grep /\S/, split /,/; print; '
> SPEED OF LIGHT, , LIGHT SPEED,TRAVEL,TRAVELLING, ,
> DANGER,DANGEROUS,PHYSICAL, , CONCEPT,CONCEPTS, , , , , , , , , , SPEED OF
> LIGHT, LIGHT SPEED,TRAVEL,TRAVELLING, DANGER,DANGEROUS,PHYSICAL,
> CONCEPT,CONCEPTS
>
>
>
>
> John
Okay I need to ask what's going on here.
I had to use the
s/,\s*(?=,)//g
expression because the
s/(\,+\s*)+/,/g;
regex in my code snip wasn't working as it did on the text snippet I
originally supplied.
=== code snip ===
while (<FH>) {
chomp($_);
s/"//g;
s/\t/, /g;
s/,\s*(?=,)//g;
print "\"$_\"\n";
}
==========
I can understand the 2nd method: A grouped, literal comma (\,), one
or more times followed by a zero or more spaces.
The 2nd regex reads to me like, a comma then zero or more spaces but
what's that (?=,) doing? Is it referring to the preceding expression
and saying if it matches up to 1 time? I can't see what the equal
sign is doing either.
Enlightment please.
Dp.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/