perl.beginners
[Top] [All Lists]

RE: Regex help

Subject: RE: Regex help
From: Beginner
Date: Mon, 03 Sep 2007 16:57:03 +0100
Newsgroups: perl.beginners

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.


<Prev in Thread] Current Thread [Next in Thread>