# Cannot remove multiple spaces

**URL:** <https://rubytalk.org/t/cannot-remove-multiple-spaces/51598>\
**Category:** ruby-talk\
**Created:** [7 February 2009 13:31 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598 "2009-02-07T13:31:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [7 February 2009 13:31 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/1 "2009-02-07T13:31:27Z")

</div>

I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!

Here is a demo of my problem, in which I try two different ways, and both fail:

=== code ===  
# h2t.rb

def main  
&nbsp;&nbsp;# conversion table spec  
&nbsp;&nbsp;conv = [  
&nbsp;&nbsp;['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
&nbsp;&nbsp;['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
&nbsp;&nbsp;[" +", ' ']] # \<= this last array element should do the trick, but doesn't

&nbsp;&nbsp;data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data )).to\_s }  
&nbsp;&nbsp;&nbsp;conv.each do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;data.gsub!( i[0], i[1] )  
&nbsp;&nbsp;end  
&nbsp;&nbsp;data.squeeze(' ') # \<= putting this here was sheer desperations, but even THIS fails

&nbsp;&nbsp;open( "h2t-out.txt", "w" ) { |f| f.write( data ) }

end

%w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib| require lib }

main

=== input file ===

\<h1\>Library catalog listing \</h1\>x

\<h3\>Library catalog listing \</h3\>x

\<h2\>Library catalog listing \</h2\>x

p(subtitle). A complete listing of all material in the Library

=== output file ===

h1. Library catalog listing x

h3. Library catalog listing x

h2. Library catalog listing x

p(subtitle). A complete listing of all material in the Library

> **···**
>
> ==============
> 
> The "x"s in the input file are to show that while the end tags are being removed the space before them is NOT.
> 
> t.
> 
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist  
> Bellingham, Washington, U.S.A: (360) 920-1226  
> \<\< [tc@tomcloyd.com](mailto:tc@tomcloyd.com) \>\> (email)  
> \<\< [TomCloyd.com](http://TomCloyd.com) \>\> (website) \<\< [sleightmind.wordpress.com](http://sleightmind.wordpress.com) \>\> (mental health weblog)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [7 February 2009 13:42 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/2 "2009-02-07T13:42:20Z")

</div>

Tom Cloyd wrote:

> I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!
> 
> Here is a demo of my problem, in which I try two different ways, and both fail:
> 
> === code ===  
> # h2t.rb
> 
> def main  
> # conversion table spec  
> conv = [  
> ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
> [" +", ' ']] # \<= this last array element should do the trick, but doesn't

Ouch. THIS - [/ +/, ' '], substituted for [" +", ' '] above fixes it. I'm going blind, obviously.

t.

> **···**
>
> > data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data )).to\_s }
> > 
> > conv.each do |i|  
> > &nbsp;&nbsp;&nbsp;data.gsub!( i[0], i[1] )  
> > end  
> > data.squeeze(' ') # \<= putting this here was sheer desperations, but even THIS fails
> > 
> > open( "h2t-out.txt", "w" ) { |f| f.write( data ) }
> > 
> > end
> > 
> > %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib| require lib }
> > 
> > main
> > 
> > === input file ===
> > 
> > \<h1\>Library catalog listing \</h1\>x
> > 
> > \<h3\>Library catalog listing \</h3\>x
> > 
> > \<h2\>Library catalog listing \</h2\>x
> > 
> > p(subtitle). A complete listing of all material in the Library
> > 
> > === output file ===
> > 
> > h1. Library catalog listing x
> > 
> > h3. Library catalog listing x
> > 
> > h2. Library catalog listing x
> > 
> > p(subtitle). A complete listing of all material in the Library
> > 
> > ==============
> > 
> > The "x"s in the input file are to show that while the end tags are being removed the space before them is NOT.
> > 
> > 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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![Craig\_Demyanovich](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Craig\_Demyanovich](https://rubytalk.org/u/Craig_Demyanovich)\
**Post date:** [7 February 2009 14:21 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/3 "2009-02-07T14:21:27Z")

</div>

See comments below.

> I'm baffled by this strange outcome - I cannot reduce multiple spaces from  
> a text file. This isn't just a regex problem, somehow. I'm failing to grasp  
> something essential, but don't know what it is. All help appreciated, as  
> usual!
> 
> Here is a demo of my problem, in which I try two different ways, and both  
> fail:
> 
> === code ===  
> # h2t.rb
> 
> def main  
> # conversion table spec  
> conv = [  
> ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [ /\<\/h\d\>/,  
> '' ],  
> [" +", ' ']] # \<= this last array element should do the trick, but  
> doesn't

The last element means replace occurrences of a space followed by a plus  
with an empty string. I assume that you were trying to write a regular  
expression, which would make your last array [/ +/, ''].

&nbsp;&nbsp;data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data )).to\_s }

> conv.each do |i|  
> &nbsp;&nbsp;data.gsub!( i[0], i[1] )  
> end  
> data.squeeze(' ') # \<= putting this here was sheer desperations, but even  
> THIS fails

This does nothing because String#squeeze returns a new string that you don't  
capture. Instead of using the array above, you could do

data = data.squeeze(' ')

> open( "h2t-out.txt", "w" ) { |f| f.write( data ) }
> 
> end
> 
> %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib|  
> require lib }
> 
> main

Hope that helps.

Regards,  
Craig

> **···**
>
> On Sat, Feb 7, 2009 at 8:31 AM, Tom Cloyd \<tomcloyd@comcast.net\> wrote:

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [7 February 2009 21:05 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/4 "2009-02-07T21:05:22Z")

</div>

Tom Cloyd wrote:

> I'm baffled by this strange outcome - I cannot reduce multiple spaces  
> from a text file. This isn't just a regex problem, somehow. I'm  
> failing to grasp something essential, but don't know what it is. All  
> help appreciated, as usual!
> 
> Here is a demo of my problem, in which I try two different ways, and  
> both fail:
> 
> === code ===  
> # h2t.rb
> 
> def main  
> &nbsp;&nbsp;# conversion table spec  
> &nbsp;&nbsp;conv = [  
> &nbsp;&nbsp;['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> &nbsp;&nbsp;['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [  
> /\<\/h\d\>/, '' ],  
> &nbsp;&nbsp;[" +", ' ']] # \<= this last array element should do the trick,  
> but doesn't
> 
> &nbsp;&nbsp;data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data )).to\_s  
> }  
> &nbsp;&nbsp;conv.each do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;data.gsub!( i[0], i[1] )  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;data.squeeze(' ') # \<= putting this here was sheer desperations,  
> but even THIS fails
> 
> &nbsp;&nbsp;open( "h2t-out.txt", "w" ) { |f| f.write( data ) }
> 
> end
> 
> %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib|  
> require lib }
> 
> main
> 
> === input file ===
> 
> \<h1\>Library catalog listing \</h1\>x
> 
> \<h3\>Library catalog listing \</h3\>x
> 
> \<h2\>Library catalog listing \</h2\>x
> 
> p(subtitle). A complete listing of all material in the Library
> 
> === output file ===
> 
> h1. Library catalog listing x
> 
> h3. Library catalog listing x
> 
> h2. Library catalog listing x
> 
> p(subtitle). A complete listing of all material in the Library
> 
> ==============
> 
> The "x"s in the input file are to show that while the end tags are  
> being removed the space before them is NOT.
> 
> t.

puts IO.readlines("data2").map{|line|  
&nbsp;&nbsp;line.sub( /\<(h\d)\>/, '\1. ' ).sub( /\<\/h\d\>/, "").  
&nbsp;&nbsp;squeeze " " }

--- output ---

h1. Library catalog listing x

h3. Library catalog listing x

h2. Library catalog listing x

p(subtitle). A complete listing of all material in the Library

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 February 2009 13:49 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/5 "2009-02-07T13:49:21Z")

</div>

Hi --

> Tom Cloyd wrote:
> 
> > I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!
> > 
> > Here is a demo of my problem, in which I try two different ways, and both fail:
> > 
> > === code ===  
> > # h2t.rb
> > 
> > def main  
> > # conversion table spec  
> > conv = [  
> > ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> > ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
> > [" +", ' ']] # \<= this last array element should do the trick, but doesn't
> 
> Ouch. THIS - [/ +/, ' '], substituted for [" +", ' '] above fixes it. I'm going blind, obviously.

Just for fun, here's another way to write the method:

def main  
&nbsp;&nbsp;data = File.read("tom.txt")  
&nbsp;&nbsp;data.gsub!(/\<(h[1-6])\>/, "\\1. ")  
&nbsp;&nbsp;data.gsub!(/\<\/h\d\>/, "")  
&nbsp;&nbsp;data.squeeze!(' ')

&nbsp;&nbsp;open("tom.out", "w") {|f| f.write(data) }

end

I think that does the same thing. Tweak to taste 🙂

David

> **···**
>
> On Sat, 7 Feb 2009, Tom Cloyd wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [7 February 2009 21:09 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/6 "2009-02-07T21:09:12Z")

</div>

William James wrote:

> Tom Cloyd wrote:
> 
> \> I'm baffled by this strange outcome - I cannot reduce multiple  
> \> spaces from a text file. This isn't just a regex problem, somehow.  
> \> I'm failing to grasp something essential, but don't know what it  
> \> is. All help appreciated, as usual!  
> \>  
> \> Here is a demo of my problem, in which I try two different ways,  
> \> and both fail:  
> \>  
> \> === code ===  
> \> # h2t.rb  
> \>  
> \> def main  
> \> # conversion table spec  
> \> conv = [  
> \> ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> \> ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [  
> \> /\<\/h\d\>/, '' ],  
> \> [" +", ' ']] # \<= this last array element should do the trick,  
> \> but doesn't  
> \>  
> \> data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data  
> \> )).to\_s }  
> \> conv.each do |i|  
> \> data.gsub!( i[0], i[1] )  
> \> end  
> \> data.squeeze(' ') # \<= putting this here was sheer desperations,  
> \> but even THIS fails  
> \>  
> \> open( "h2t-out.txt", "w" ) { |f| f.write( data ) }  
> \>  
> \> end  
> \>  
> \> %w(rubygems ruby-debug readline strscan logger fileutils).each{  
> \> \>lib\> require lib }  
> \>  
> \> main  
> \>  
> \> === input file ===  
> \>  
> \> \<h1\>Library catalog listing \</h1\>x  
> \>  
> \> \<h3\>Library catalog listing \</h3\>x  
> \>  
> \> \<h2\>Library catalog listing \</h2\>x  
> \>  
> \> p(subtitle). A complete listing of all material in the Library  
> \>  
> \>  
> \> === output file ===  
> \>  
> \>  
> \> h1. Library catalog listing x  
> \>  
> \> h3. Library catalog listing x  
> \>  
> \> h2. Library catalog listing x  
> \>  
> \> p(subtitle). A complete listing of all material in the Library  
> \>  
> \> ==============  
> \>  
> \> The "x"s in the input file are to show that while the end tags are  
> \> being removed the space before them is NOT.  
> \>  
> \> t.
> 
> puts IO.readlines("data2").map{|line|  
> &nbsp;&nbsp;line.sub( /\<(h\d)\>/, '\1. ' ).sub( /\<\/h\d\>/, "").  
> &nbsp;&nbsp;squeeze " " }
> 
> --- output ---
> 
> h1. Library catalog listing x
> 
> h3. Library catalog listing x
> 
> h2. Library catalog listing x
> 
> p(subtitle). A complete listing of all material in the Library

puts IO.read("data2").gsub( /\<(h\d)\>/, '\1. ' ).gsub( /\<\/h\d\>/, "").  
&nbsp;&nbsp;squeeze " "

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [7 February 2009 20:50 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/7 "2009-02-07T20:50:01Z")

</div>

David A. Black wrote:

> Hi --
> 
> > Tom Cloyd wrote:
> > 
> > > I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!
> > > 
> > > Here is a demo of my problem, in which I try two different ways, and both fail:
> > > 
> > > === code ===  
> > > # h2t.rb
> > > 
> > > def main  
> > > # conversion table spec  
> > > conv = [  
> > > ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> > > ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
> > > [" +", ' ']] # \<= this last array element should do the trick, but doesn't
> > 
> > Ouch. THIS - [/ +/, ' '], substituted for [" +", ' '] above fixes it. I'm going blind, obviously.
> 
> Just for fun, here's another way to write the method:
> 
> def main  
> data = File.read("tom.txt")  
> data.gsub!(/\<(h[1-6])\>/, "\\1. ")  
> data.gsub!(/\<\/h\d\>/, "")  
> data.squeeze!(' ')
> 
> open("tom.out", "w") {|f| f.write(data) }
> 
> end
> 
> I think that does the same thing. Tweak to taste 🙂
> 
> David

That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:

data.gsub!(/\<(h[1-6])\>/, "\\1. ")

It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?

Thanks!

Tom

> **···**
>
> > On Sat, 7 Feb 2009, Tom Cloyd wrote:
> 
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> 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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 February 2009 20:53 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/8 "2009-02-07T20:53:56Z")

</div>

Hi --

> **···**
>
> On Sun, 8 Feb 2009, Tom Cloyd wrote:
> 
> > David A. Black wrote:
> > 
> > > Hi --
> > > 
> > > On Sat, 7 Feb 2009, Tom Cloyd wrote:
> > > 
> > > > Tom Cloyd wrote:
> > > > 
> > > > > I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!
> > > > > 
> > > > > Here is a demo of my problem, in which I try two different ways, and both fail:
> > > > > 
> > > > > === code ===  
> > > > > # h2t.rb
> > > > > 
> > > > > def main  
> > > > > # conversion table spec  
> > > > > conv = [  
> > > > > ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> > > > > ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
> > > > > [" +", ' ']] # \<= this last array element should do the trick, but doesn't
> > > > 
> > > > Ouch. THIS - [/ +/, ' '], substituted for [" +", ' '] above fixes it. I'm going blind, obviously.
> > > 
> > > Just for fun, here's another way to write the method:
> > > 
> > > def main  
> > > data = File.read("tom.txt")  
> > > data.gsub!(/\<(h[1-6])\>/, "\\1. ")  
> > > data.gsub!(/\<\/h\d\>/, "")  
> > > data.squeeze!(' ')
> > > 
> > > open("tom.out", "w") {|f| f.write(data) }
> > > 
> > > end
> > > 
> > > I think that does the same thing. Tweak to taste 🙂
> > > 
> > > David
> > 
> > That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> > 
> > data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> > 
> > It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?
> 
> The \\1, \\2, etc. in the replacement string are pegged to the  
> parenthetical captures. "\\1. " means: the first capture (which is h  
> plus a digit), a period, and a space.
> 
> They work in single-quoted strings too, but there they're just \1, \2,  
> etc. There's some explanation in the ri docs for String#gsub.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![Jan-Erik\_R](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jan-Erik\_R](https://rubytalk.org/u/Jan-Erik_R)\
**Post date:** [7 February 2009 20:54 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/9 "2009-02-07T20:54:03Z")

</div>

Tom Cloyd schrieb:

> David A. Black wrote:
> 
> > Hi --
> > 
> > > Tom Cloyd wrote:
> > > 
> > > > I'm baffled by this strange outcome - I cannot reduce multiple spaces from a text file. This isn't just a regex problem, somehow. I'm failing to grasp something essential, but don't know what it is. All help appreciated, as usual!
> > > > 
> > > > Here is a demo of my problem, in which I try two different ways, and both fail:
> > > > 
> > > > === code ===  
> > > > # h2t.rb
> > > > 
> > > > def main  
> > > > # conversion table spec  
> > > > conv = [  
> > > > ['\<h1\>', 'h1. '], ['\<h2\>', 'h2. '], ['\<h3\>', 'h3. '],  
> > > > ['\<h4\>', 'h4. '], ['\<h5\>', 'h5. '], ['\<h6\>', 'h6. '], [/\<\/h\d\>/, ''],  
> > > > [" +", ' ']] # \<= this last array element should do the trick, but doesn't
> > > 
> > > Ouch. THIS - [/ +/, ' '], substituted for [" +", ' '] above fixes it. I'm going blind, obviously.
> > 
> > Just for fun, here's another way to write the method:
> > 
> > def main  
> > data = File.read("tom.txt")  
> > data.gsub!(/\<(h[1-6])\>/, "\\1. ")  
> > data.gsub!(/\<\/h\d\>/, "")  
> > data.squeeze!(' ')
> > 
> > open("tom.out", "w") {|f| f.write(data) }
> > 
> > end
> > 
> > I think that does the same thing. Tweak to taste 🙂
> > 
> > David
> 
> That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> 
> data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> 
> It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?
> 
> Thanks!
> 
> Tom

ah...regex! it's easy if you know them =D  
the (...) in the Regex defines a group.  
this group now includes the 'h' followed by one of the numbers 1,2,3,4,5 or 6  
in the second parameter \1 (double slash because of double-quotes/escaping 😉 now is assgined to the matched pattern /h[1-6]/  
that's it, nothing magic anymore 😉

> **···**
>
> > > On Sat, 7 Feb 2009, Tom Cloyd wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [7 February 2009 20:59 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/10 "2009-02-07T20:59:18Z")

</div>

The keyword is "capturing groups". Brackets in the regexp denote groups of characters which can be referenced later via their numeric index as you have seen. You can even use them for matching repetitions

/(fo+)\1/ =~ s # will match "fofo", "foofoo", "fooofooo" etc.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 07.02.2009 21:50, Tom Cloyd wrote:
> 
> > That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> > 
> > data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> > 
> > It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [7 February 2009 21:48 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/11 "2009-02-07T21:48:08Z")

</div>

Robert Klemme wrote:

> > That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> > 
> > data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> > 
> > It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?
> 
> The keyword is "capturing groups". Brackets in the regexp denote groups of characters which can be referenced later via their numeric index as you have seen. You can even use them for matching repetitions
> 
> /(fo+)\1/ =~ s # will match "fofo", "foofoo", "fooofooo" etc.
> 
> Cheers
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

David, badboy, Robert - thats to you all for the very clear explanations. I really couldn't find info. about this (yet). It IS clear, once the explanation's in had. I have to say that regex's becoming rather fun, now that I'm getting a little control of it.

I continue to be amazed at the generosity of this list in helping the real amateurs here move things along. We get that AND we get to listen in on all sorts of amazing and mysterious discussions of higher order magic. Pretty cool.

t.

> **···**
>
> > On 07.02.2009 21:50, Tom Cloyd wrote:
> 
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> 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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [7 February 2009 21:53 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/12 "2009-02-07T21:53:13Z")

</div>

Robert Klemme wrote:

> **···**
>
> > On 07.02.2009 21:50, Tom Cloyd wrote:
> > 
> > > That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> > > 
> > > data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> > > 
> > > It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?
> > 
> > The keyword is "capturing groups". Brackets in the regexp denote groups of characters which can be referenced later via their numeric index as you have seen. You can even use them for matching repetitions
> > 
> > /(fo+)\1/ =~ s # will match "fofo", "foofoo", "fooofooo" etc.
> > 
> > Cheers
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert
> 
> I should have added this - as it was puzzling me and I just now "got it" (and no one mentioned it) - for those who might be following along or will come after: the "\1" business isn't regex. That's why I could find nothing about it in my regex sources! It's a String#gsub() convention, and is documented there.
> 
> OK...all darkness is vanquished. For now. (!) ~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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![MOUBAMBA\_Laury\_karin](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@MOUBAMBA\_Laury\_karin](https://rubytalk.org/u/MOUBAMBA_Laury_karin)\
**Post date:** [8 February 2009 09:20 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/13 "2009-02-08T09:20:24Z")

</div>

how to unsubscribe to this email list ?

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> De : Tom Cloyd \<tomcloyd@comcast.net\>  
> À : ruby-talk ML \<ruby-talk@ruby-lang.org\>  
> Envoyé le : Samedi, 7 Février 2009, 22h48mn 08s  
> Objet : Re: cannot remove multiple space\> nuts!
> 
> Robert Klemme wrote:
> 
> > On 07.02.2009 21:50, Tom Cloyd wrote:
> > 
> > > That's beautifully economical, and reveals a far better grasp of regex than I was able to attain last night. However, I'm having trouble with this line:
> > > 
> > > data.gsub!(/\<(h[1-6])\>/, "\\1. ")
> > > 
> > > It certain works, but I don't grasp the "\\1. " part. I haven't yet found anything that might shed light on this magic. How does it retain the 'h' and whatever digit follows it? It looks somehow like "\\" == retain matched alpha, and the "1" does the same for matched digits, but I really haven't a clue. Can you elucidate just a bit?
> > 
> > The keyword is "capturing groups". Brackets in the regexp denote groups of characters which can be referenced later via their numeric index as you have seen. You can even use them for matching repetitions
> > 
> > /(fo+)\1/ =~ s # will match "fofo", "foofoo", "fooofooo" etc.
> > 
> > Cheers
> > 
> > ```
> > robert
> > 
> > ```
> 
> David, badboy, Robert - thats to you all for the very clear explanations. I really couldn't find info. about this (yet). It IS clear, once the explanation's in had. I have to say that regex's becoming rather fun, now that I'm getting a little control of it.
> 
> I continue to be amazed at the generosity of this list in helping the real amateurs here move things along. We get that AND we get to listen in on all sorts of amazing and mysterious discussions of higher order magic. Pretty cool.
> 
> 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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Ne pleurez pas si votre Webmail ferme ! Récupérez votre historique sur Yahoo! Mail ! [Yahoo fait partie de la famille de marques Yahoo.](http://fr.docs.yahoo.com/mail/transfert_mails.html)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [8 February 2009 12:44 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/14 "2009-02-08T12:44:08Z")

</div>

If you like to dig deeper into the matter, I can recommend [http://oreilly.com/catalog/9780596528126/index.html](http://oreilly.com/catalog/9780596528126/index.html)

I covers functionality, different regexp dialects and performance considerations thoroughly. Might be a bit difficult to read if you do not have a full CS background but IMHO Jeff Friedl manages to keep language theory at a minimum without scarifying precision.

And one more particular Ruby hint: method String# is capable of working with regular expression arguments, so you can do

# fetch the whole match  
ip = input[/\d{1,3}(\.\d{1,3}){3}/]

# fetch group 1  
name = input[/name=(\S+)/, 1]

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 07.02.2009 22:48, Tom Cloyd wrote:
> 
> > I have to say that regex's becoming rather fun, now that I'm getting a little control of it.

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [8 February 2009 18:05 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/15 "2009-02-08T18:05:12Z")

</div>

If you want an introductory tutorials about regular expressions you  
can check here:

> **[Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp...](https://www.regular-expressions.info/)**
>
> At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.

Jesus.

> **···**
>
> On Sun, Feb 8, 2009 at 1:44 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 07.02.2009 22:48, Tom Cloyd wrote:
> > 
> > > I have to say that regex's becoming rather fun, now that I'm getting a  
> > > little control of it.
> > 
> > If you like to dig deeper into the matter, I can recommend  
> > [http://oreilly.com/catalog/9780596528126/index.html](http://oreilly.com/catalog/9780596528126/index.html)
> > 
> > I covers functionality, different regexp dialects and performance  
> > considerations thoroughly. Might be a bit difficult to read if you do not  
> > have a full CS background but IMHO Jeff Friedl manages to keep language  
> > theory at a minimum without scarifying precision.

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [9 February 2009 06:32 UTC](https://rubytalk.org/t/cannot-remove-multiple-spaces/51598/16 "2009-02-09T06:32:58Z")

</div>

Jesús Gabriel y Galán wrote:

> **···**
>
> > On Sun, Feb 8, 2009 at 1:44 PM, Robert Klemme \> \<shortcutter@googlemail.com\> wrote:  
> > &nbsp;&nbsp;
> > 
> > > On 07.02.2009 22:48, Tom Cloyd wrote:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;
> > > 
> > > > I have to say that regex's becoming rather fun, now that I'm getting a  
> > > > little control of it.  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > > 
> > > If you like to dig deeper into the matter, I can recommend  
> > > [Mastering Regular Expressions, 3rd Edition [Book]](http://oreilly.com/catalog/9780596528126/index.html)
> > > 
> > > I covers functionality, different regexp dialects and performance  
> > > considerations thoroughly. Might be a bit difficult to read if you do not  
> > > have a full CS background but IMHO Jeff Friedl manages to keep language  
> > > theory at a minimum without scarifying precision.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > If you want an introductory tutorials about regular expressions you  
> > > can check here:  
> > > [http://www.regular-expressions.info](http://www.regular-expressions.info)
> > 
> > Jesus.
> 
> Robert, Jesus,  
> Thanks to you both. Great stuff. I'll be digging into it tonight.
> 
> 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)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
