Forum: Ruby regex \s == \n???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-06 13:12
(Received via mailing list)
I'm trying to remove extra spaces from a long string which has some
EOLs, using regex. It's not working. Here's a simple demo:

irb(main):004:0> a="\n  abc\n  a  a  a"
=> "\n  abc\n  a  a  a"
irb(main):005:0> a.gsub(/\s+/,' ')
=> " abc a a a"

I've dug around in my regex references, and all I can say is that is
hasn't been the least bit helpful. I'm probably not looking for the
right thing.

Can someone more knowledgeable tell me is there's a way to do this -
remove extra spaces without removing the EOLs?

Thanks!

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-02-06 13:23
(Received via mailing list)
Alle Friday 06 February 2009, Tom Cloyd ha scritto:
> right thing.
>
> Can someone more knowledgeable tell me is there's a way to do this -
> remove extra spaces without removing the EOLs?
>
> Thanks!
>
> t.

According to "The Ruby Programming Language", \s is equivalent to "
\t\n\r\f".
So, if you want avoid removing newlines, you'll need to replace \s with
[ \t\r\f] or with a whitespace if you're only intersted in it:

a="\n  abc\n  a  a  a"
a.gsub(/ +/, ' ')
=>"\n abc\n a a a"

I hope this helps

Stefano
9d852f868cd85f6db2eb702a464095e5?d=identicon&s=25 joe chesak (Guest)
on 2009-02-06 13:26
(Received via mailing list)
Tom,

If you're just speaking of the space character and you want to replace
double-spaces (or triple-spaces or more) with just a single space, you
can
do this.

puts a.gsub(/  +/," ")

Joe
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-06 13:29
(Received via mailing list)
Stefano, Joe - thank you! I'm only just getting into regex, so I get
easily lost. You solved my problem - each in different ways. A lot of
bang for the buck, indeed!

t.

joe chesak wrote:
> On Fri, Feb 6, 2009 at 1:10 PM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>> I've dug around in my regex references, and all I can say is that is hasn't
>>
>>
>
>


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-06 13:39
(Received via mailing list)
Hi --

On Fri, 6 Feb 2009, Tom Cloyd wrote:

> Stefano, Joe - thank you! I'm only just getting into regex, so I get easily
> lost. You solved my problem - each in different ways. A lot of bang for the
> buck, indeed!

Another variant:

   a.gsub(/[^\S\n]+/, " ")

That character class means "all characters that are not a non-space or
\n." (The ^ is the "not" part.)

You might also be able to use squeeze:

   p "abc    def   \n   ghi\n".squeeze   # "abc def \n ghi\n"

though that's going to be less versatile if you're dealing, say, with
tabs.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-02-06 13:45
(Received via mailing list)
On Feb 6, 7:10 am, Tom Cloyd <tomcl...@comcast.net> wrote:
> right thing.
A newline is a whitespace char. \s is the same as [ \t\r\n\f]. If you
don't want to match them, remove them. Try
a.gsub(/[ \t]+/,' ')

--Mark
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-06 15:38
(Received via mailing list)
Can't you use squeeze?

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-02-06 19:25
(Received via mailing list)
On Feb 6, 9:37 am, Julian Leviston <jul...@coretech.net.au> wrote:
> Can't you use squeeze?

Best idea yet. Might as well use a built-in, rather than reinventing
one.

a.squeeze(" ")

Thanks for the reminder. I should review String#instance_methods every
once in a while. There's some good stuff there.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-06 19:37
(Received via mailing list)
Hi --

On Sat, 7 Feb 2009, Mark Thomas wrote:

> On Feb 6, 9:37 am, Julian Leviston <jul...@coretech.net.au> wrote:
>> Can't you use squeeze?
>
> Best idea yet. Might as well use a built-in, rather than reinventing
> one.
>
> a.squeeze(" ")

See my earlier post in this thread. squeeze is a good candidate as
long as you don't mind preserving all non-" " whitespace, including
tabs.

(squeeze defaults to " " as its argument, so you don't have to provide
an argument unless it's something different.)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2009-02-06 21:29
David A. Black wrote:
> Hi --

> (squeeze defaults to " " as its argument, so you don't have to provide
> an argument unless it's something different.)
>
>
> David

Is that a 1.9.1 change? In 1.8.6 String#squeeze squeezes everything if
no arguments are given.

"abc aabbcc  ".squeeze
#=>"abc abc "

Siep
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-06 21:46
(Received via mailing list)
Hi --

On Sat, 7 Feb 2009, Siep Korteling wrote:

> no arguments are given.
>
> "abc aabbcc  ".squeeze
> #=>"abc abc "

Sorry, my mistake. It does squeeze everything.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-06 23:10
(Received via mailing list)
David A. Black wrote:
>   a.gsub(/[^\S\n]+/, " ")
>
>
> David
>
Thanks, David. I continue to be amazed by the depth of your knowledge,
and outright cleverness. In pursuing this simply problem I'm
inadvertently learning a lot. I'm grateful. Thanks for your contribution
that process!

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-07 01:42
(Received via mailing list)
Sorry, forgot the " " argument

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
This topic is locked and can not be replied to.