# Re: removing spaces between single characters with regual ex

**URL:** <https://www.ruby-forum.com/t/re-removing-spaces-between-single-characters-with-regual-ex/68443>\
**Category:** Ruby\
**Created:** [September 12, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/re-removing-spaces-between-single-characters-with-regual-ex/68443 "2006-09-12T20:03:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 12, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/re-removing-spaces-between-single-characters-with-regual-ex/68443/1 "2006-09-12T20:03:37Z")

</div>

From: ciapecki [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> ‘A B C’ → ‘ABC’  
> ‘AB C’ → ‘AB C’ (no change)  
> ‘Teddy Bear A B’ → ‘Teddy Bear AB’

irb(main):005:0\> s = ‘A long cat and a quick mouse met o j simpson on  
the t c p stack’  
=\> “A long cat and a quick mouse met o j simpson on the t c p stack”

irb(main):006:0\> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, ‘\1’ )  
=\> “A long cat and a quick mouse met oj simpson on the tcp stack”

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 12, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/re-removing-spaces-between-single-characters-with-regual-ex/68443/2 "2006-09-12T21:31:53Z")

</div>

Thanks Gavin for your solution

> irb(main):005:0\> s = ‘A long cat and a quick mouse met o j simpson on  
> the t c p stack’  
> =\> “A long cat and a quick mouse met o j simpson on the t c p stack”
> 
> irb(main):006:0\> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, ‘\1’ )  
> =\> “A long cat and a quick mouse met oj simpson on the tcp stack”

this is exactly what I need.  
Is it possible to rewrite it without using lookahead (?=subexp) ?

chris

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 13, 2006, 12:26am UTC](https://www.ruby-forum.com/t/re-removing-spaces-between-single-characters-with-regual-ex/68443/3 "2006-09-13T00:26:11Z")

</div>

> irb(main):006:0\> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, ‘\1’ )  
> =\> “A long cat and a quick mouse met oj simpson on the tcp stack”

what about to add some additional characters like ‘&’ to work as  
characters?  
to make following possible:  
“a b & c de” =\> “ab&c de”

chris
