one regex to do the work of two?

S

Spydo

I frequently have to clean up lines with:

s/^\s+//;
s/\s+$//;

Is there ONE regex (as in ONE, SINGLE s///, not one compound
statement) that can do both of these ?

Thanks & happy Gnu Year.
 
S

Spydo

I frequently have to clean up lines with:

s/^\s+//;
s/\s+$//;

Is there ONE regex (as in ONE, SINGLE s///, not one compound
statement) that can do both of these ?

Thanks & happy Gnu Year.

DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..

s/(^\s+)|(\s+$)//g;
 
R

Randal L. Schwartz

Spydo> s/^\s+//;
Spydo> s/\s+$//;

Spydo> Is there ONE regex (as in ONE, SINGLE s///, not one compound
Spydo> statement) that can do both of these ?

Only if you sacrifice runtime efficiency. :)
 
J

Jürgen Exner

Spydo said:
I frequently have to clean up lines with:

s/^\s+//;
s/\s+$//;

Is there ONE regex (as in ONE, SINGLE s///, not one compound
statement) that can do both of these ?

Yes, there is but it doesn't make much sense to use it.
See the relevent FAQ
perldoc -q "strip blank"
for details.

jue
 
J

John W. Krahn

Spydo said:
DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..

s/(^\s+)|(\s+$)//g;

There is no need for the parentheses

s/^\s+|\s+$//g;

Does the same without having to save data to $1 and $2.



John
 
K

Kevin Ryde

Spydo said:
s/(^\s+)|(\s+$)//g;

Or Regexp::Common::whitespace for a big way of getting a little
expression. Text::Trim is effective too, in a scary context-sensitive
do-what-i-mean way.

(Could the faq cross reference those? If it doesn't make a long entry
even longer ... :)
 
G

George Mpouras

Στις 19/1/2011 8:28 μμ, ο/η Spydo έγÏαψε:
DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..

s/(^\s+)|(\s+$)//g;


here is a pretty one

s/^\s*(.*?)\s*$/$1/
 
D

Dr.Ruud

That's really neat.

I don't think so, because it also replaces when there is no heading or
tailing whitespace at all.

I normally use:

s/\s+\z//, # remove trailing whitespace
s/\A\s+//, # remove leading whitespace
s/[^\S ]/ /g, # replace non-space whitespace by space
s/ {2,}/ /g, # unify spaces
for $input;

See also tr/ in perlop, think about tr/\t\n / /s.
 
G

George Mpouras

Στις 9/2/2011 3:07 πμ, ο/η Dr.Ruud έγÏαψε:
That's really neat.

I don't think so, because it also replaces when there is no heading or
tailing whitespace at all.

I normally use:

s/\s+\z//, # remove trailing whitespace
s/\A\s+//, # remove leading whitespace
s/[^\S ]/ /g, # replace non-space whitespace by space
s/ {2,}/ /g, # unify spaces
for $input;

See also tr/ in perlop, think about tr/\t\n / /s.


too much noise for something simple
 
S

sln

That's really neat.

I don't think so, because it also replaces when there is no heading or
tailing whitespace at all.

I normally use:

s/\s+\z//, # remove trailing whitespace
s/\A\s+//, # remove leading whitespace
s/[^\S ]/ /g, # replace non-space whitespace by space
s/ {2,}/ /g, # unify spaces
for $input;

See also tr/ in perlop, think about tr/\t\n / /s.

This is pretty far afield from removing leading/trailing
whitespace.

-sln
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top