# Clean a header

**URL:** <https://www.ruby-forum.com/t/clean-a-header/132909>\
**Category:** Ruby\
**Created:** [March 25, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/clean-a-header/132909 "2008-03-25T18:53:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 25, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/clean-a-header/132909/1 "2008-03-25T18:53:01Z")

</div>

Hi, I’ve a string like this:

header = “Header : \r\n \t \r\n \t 111 aaa 2222 3333 \r\n”

I need to clean it and leave as follows:

“Header:111 aaa 2222 3333”

The best I get is this:

header.strip.gsub(/(\s\*)/,"")  
=\> “Header:111aaa22223333”

Obviously this is not valid since it “eats” the spaces into the header  
body. How could I fix it?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 25, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/clean-a-header/132909/2 "2008-03-25T19:14:28Z")

</div>

On Mar 25, 2008, at 1:51 PM, Iñaki Baz C. wrote:

> header.strip.gsub(/(\s\*)/,“”)  
> =\> “Header:111aaa22223333”
> 
> Obviously this is not valid since it “eats” the spaces into the header  
> body. How could I fix it?
> 
> ## Thanks in advance.
> 
> Iñaki Baz C.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

irb\> header = “Header : \r\n \t \r\n \t 111 aaa 2222 3333  
\r\n”  
=\> “Header : \r\n \t \r\n \t 111 aaa 2222 3333 \r\n”  
irb\> header.strip.sub(/\s\*:\s\*/,‘:’).gsub(/\s+/,’ ')  
=\> “Header:111 aaa 2222 3333”

Good enough?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [March 25, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/clean-a-header/132909/3 "2008-03-25T20:58:34Z")

</div>

El Martes, 25 de Marzo de 2008, Rob B.  
escribió:

> irb\> header = “Header : \r\n \t \r\n \t 111 aaa 2222 3333  
> \r\n”  
> =\> “Header : \r\n \t \r\n \t 111 aaa 2222 3333 \r\n”  
> irb\> header.strip.sub(/\s\*:\s\*/,’:’).gsub(/\s+/,’ ')  
> =\> “Header:111 aaa 2222 3333”
> 
> Good enough?

No good, it’s fantastic 🙂

Thanks a lot.
