Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > one regex to do the work of two?

Reply
Thread Tools

one regex to do the work of two?

 
 
Spydo
Guest
Posts: n/a
 
      01-19-2011
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.
 
Reply With Quote
 
 
 
 
Spydo
Guest
Posts: n/a
 
      01-19-2011
On Jan 19, 1:24*pm, Spydo <spy...@gmail.com> wrote:
> 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;

 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      01-19-2011
>>>>> "Spydo" == Spydo <> writes:

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.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      01-20-2011
Spydo <> wrote:
>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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-20-2011
Spydo wrote:
> On Jan 19, 1:24 pm, Spydo<spy...@gmail.com> wrote:
>> 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;


There is no need for the parentheses

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

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



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
Kevin Ryde
Guest
Posts: n/a
 
      01-20-2011
Spydo <> writes:
>
> 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 ...


--
Some people say cricket is like watching grass grow.
That's not true, it's watching grass die from not watering it.
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      02-07-2011
Στις 19/1/2011 8:28 μμ, ο/η Spydo *γραψε:
> On Jan 19, 1:24 pm, Spydo<spy...@gmail.com> wrote:
>> 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;
>



here is a pretty one

s/^\s*(.*?)\s*$/$1/
 
Reply With Quote
 
David Canzi
Guest
Posts: n/a
 
      02-09-2011
In article <iipnm2$1l35$>,
George Mpouras <> wrote:
>Στις 19/1/2011 8:28 μμ, ο/η Spydo *γραψε:
>> On Jan 19, 1:24 pm, Spydo<spy...@gmail.com> wrote:
>>> 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;
>>

>
>
>here is a pretty one
>
>s/^\s*(.*?)\s*$/$1/


That's really neat.

--
David Canzi | Life is too short to point out every mistake. |
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      02-09-2011
On 2011-02-09 01:34, David Canzi wrote:
> In article<iipnm2$1l35$>,
> George Mpouras<> wrote:
>> Στις 19/1/2011 8:28 μμ, ο/η Spydo *γραψε:
>>> On Jan 19, 1:24 pm, Spydo<spy...@gmail.com> wrote:


>>>> 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;
>>>

>>
>>
>> here is a pretty one
>>
>> s/^\s*(.*?)\s*$/$1/

>
> 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.

--
Ruud
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      02-09-2011
Στις 9/2/2011 3:07 πμ, ο/η Dr.Ruud *γραψε:
> On 2011-02-09 01:34, David Canzi wrote:
>> In article<iipnm2$1l35$>,
>> George Mpouras<> wrote:
>>> Στις 19/1/2011 8:28 μμ, ο/η Spydo Î*γραψε:
>>>> On Jan 19, 1:24 pm, Spydo<spy...@gmail.com> wrote:

>
>>>>> 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;
>>>>
>>>
>>>
>>> here is a pretty one
>>>
>>> s/^\s*(.*?)\s*$/$1/

>>
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Why does one regex routine work and not the other one? TtfnJohn Python 1 06-11-2007 07:17 AM
Why does one regex and the other not work?? tlyczko Javascript 7 12-12-2005 08:29 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57