looking for a better regexp

M

mike

hi folks

i have to strip a line of empty spaces/tabs before and after a comma
eg word1, word2 ,word3,word4<tab>,word5
i have to make it become word1,word2,word3,word4,word5

so far i have tried
$line =~ s/^\s+//; #get rid of spaces in the beginining of the line
$line =~ s/^\t+//; #get rid of tabs in the beginning of the line
$line =~ s/\s+,/,/g ; #get rid of spaces before a comma
$line =~ s/,\s+/,/g; #get rid of spaces after a comma
$line =~ s/\t+,/,/g; #get rid of tables before comma
$line =~ s/,\t+/,/g; #get rid of tabs after comma

i may have the line also containing eg
word1, word2 <2 tabs> ,word3,word4<1 tab><space><space>,word5
or other combinations of spaces/tabs .

Is there a better way to do the regexp? I just need the output to be
word1,word2,word3,word4,word5

thanks for any help
 
G

Gunnar Hjalmarsson

mike said:
i have to strip a line of empty spaces/tabs before and after a comma
eg word1, word2 ,word3,word4<tab>,word5
i have to make it become word1,word2,word3,word4,word5

so far i have tried
$line =~ s/^\s+//; #get rid of spaces in the beginining of the line
$line =~ s/^\t+//; #get rid of tabs in the beginning of the line
$line =~ s/\s+,/,/g ; #get rid of spaces before a comma
$line =~ s/,\s+/,/g; #get rid of spaces after a comma
$line =~ s/\t+,/,/g; #get rid of tables before comma
$line =~ s/,\t+/,/g; #get rid of tabs after comma

i may have the line also containing eg
word1, word2 <2 tabs> ,word3,word4<1 tab><space><space>,word5
or other combinations of spaces/tabs .

Is there a better way to do the regexp? I just need the output to be
word1,word2,word3,word4,word5

Probably. The \s character class includes tabs, so this should be
sufficient:

$line =~ s/^\s+//;
$line =~ s/\s*,\s*/,/g;

Or if the words may not include whitespace, i.e. if you simply want to
remove all spaces and tab characters:

$line =~ tr/ \t//d;
 
J

John W. Krahn

mike said:
i have to strip a line of empty spaces/tabs before and after a comma
eg word1, word2 ,word3,word4<tab>,word5
i have to make it become word1,word2,word3,word4,word5

so far i have tried
$line =~ s/^\s+//; #get rid of spaces in the beginining of the line
$line =~ s/^\t+//; #get rid of tabs in the beginning of the line
$line =~ s/\s+,/,/g ; #get rid of spaces before a comma
$line =~ s/,\s+/,/g; #get rid of spaces after a comma
$line =~ s/\t+,/,/g; #get rid of tables before comma
$line =~ s/,\t+/,/g; #get rid of tabs after comma

i may have the line also containing eg
word1, word2 <2 tabs> ,word3,word4<1 tab><space><space>,word5
or other combinations of spaces/tabs .

Is there a better way to do the regexp? I just need the output to be
word1,word2,word3,word4,word5

You could do it like this:

$line = join ',', map { s/^\s+//; s/\s+$//; $_ } split /,/, $line;


John
 
L

Leon

mike said:
hi folks

i have to strip a line of empty spaces/tabs before and after a comma
eg word1, word2 ,word3,word4<tab>,word5
i have to make it become word1,word2,word3,word4,word5

so far i have tried
$line =~ s/^\s+//; #get rid of spaces in the beginining of the line
$line =~ s/^\t+//; #get rid of tabs in the beginning of the line
$line =~ s/\s+,/,/g ; #get rid of spaces before a comma
$line =~ s/,\s+/,/g; #get rid of spaces after a comma
$line =~ s/\t+,/,/g; #get rid of tables before comma
$line =~ s/,\t+/,/g; #get rid of tabs after comma

i may have the line also containing eg
word1, word2 <2 tabs> ,word3,word4<1 tab><space><space>,word5
or other combinations of spaces/tabs .

Is there a better way to do the regexp? I just need the output to be
word1,word2,word3,word4,word5

thanks for any help

The following will get rid of all \t and whitespaces (note : there is a
space (" ") immediately after the square bracketed \t ).

$line =~ s/[\t ]//g;

This will return correctly for above examples, but assume there are no
whitespaces within "word1", "word2", etc..

Hope this is of some help.
 
L

Leon

Tad McClellan said:
Leon said:
$line =~ s/[\t ]//g;


This will do the same thing, only faster:

$line =~ tr/\t //d;

Thanks for that Tad:) (always interested in learning some new) Out of
curiousity, does that apply on all occasions when performing pattern
matching that would work using either ~s~ or ~tr~? (Is ~tr~ always gonna be
faster?)
 
T

Tad McClellan

Leon said:
Tad McClellan said:
Leon said:
$line =~ s/[\t ]//g;


This will do the same thing, only faster:

$line =~ tr/\t //d;

Thanks for that Tad:) (always interested in learning some new) Out of
curiousity, does that apply on all occasions when performing pattern
matching that would work using either ~s~ or ~tr~?


Your question does not make sense as phrased, because there
is NO pattern matching going on with tr/// despite the fact
that it looks an awful lot like a s/// and can be used with
the binding operator.

(Is ~tr~ always gonna be
faster?)


Yes, without a doubt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top