# Removing optional white space

**URL:** <https://www.ruby-forum.com/t/removing-optional-white-space/179302>\
**Category:** Ruby\
**Created:** [November 21, 2009, 4:03pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302 "2009-11-21T16:03:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 4:03pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/1 "2009-11-21T16:03:05Z")

</div>

People,

I want to change:

To: phil

or:

To:phil

into:

phil

- I can do this with two gsubs - is it possible to do it with one?

Thanks,

## Phil.

Philip R.

GPO Box 3411  
Sydney NSW 2001  
Australia  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/2 "2009-11-21T16:07:11Z")

</div>

People,

Looked at this for ages, finally sent the note to the list and then  
worked out how to do it minutes later . .

FYI: string.gsub( /^…:\s?/, ‘’ )

Phil.

On 2009-11-22 01:54, Philip R. wrote:

> into:
> 
> phil
> 
> - I can do this with two gsubs - is it possible to do it with one?
> 
> Thanks,
> 
> Phil.

–  
Philip R.

GPO Box 3411  
Sydney NSW 2001  
Australia  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/3 "2009-11-21T16:13:37Z")

</div>

On Saturday 21 November 2009, Philip R. wrote:

> |into:  
> |  
> | phil  
> |  
> |- I can do this with two gsubs - is it possible to do it with one?  
> |  
> |Thanks,  
> |  
> |Phil.  
> |

This should work. The regexp searches for the string To: followed by any  
number of whitespaces (including 0) followed by a letter. Since a  
positive  
look-haead is used for the letter, it isn’t replaced.

s1 = “To: phil”  
s2 = “To:phil”  
reg = /To:\s\*(?=\w)/  
s1.gsub(reg,’’)  
s2.gsub(reg,’’)

If you are sure that there can be at most one whitespace after the :,  
you can  
replace \s\* (which matches any number of whitespaces) with \s?, which  
matches  
zero or one whitespaces.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/4 "2009-11-21T16:18:54Z")

</div>

People,

For more than one white space char:

string.gsub( /^…:\s\*/, ‘’ )

Phil.

On 2009-11-22 02:04, Philip R. wrote:

> On 2009-11-22 01:54, Philip R. wrote:
> 
> > into:
> > 
> > phil
> > 
> > - I can do this with two gsubs - is it possible to do it with one?
> > 
> > Thanks,
> > 
> > Phil.

–  
Philip R.

GPO Box 3411  
Sydney NSW 2001  
Australia  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/5 "2009-11-21T17:58:23Z")

</div>

Philip R. wrote:

> People,
> 
> For more than one white space char:
> 
> string.gsub( /^…:\s\*/, ‘’ )

Actually, \* is zero or more. For one or more, use + .

> Phil.
> 
> On 2009-11-22 02:04, Philip R. wrote:
> 
> > On 2009-11-22 01:54, Philip R. wrote:
> > 
> > > into:
> > > 
> > > phil
> > > 
> > > - I can do this with two gsubs - is it possible to do it with one?
> > > 
> > > Thanks,
> > > 
> > > Phil.
> 
> –  
> Philip R.
> 
> GPO Box 3411  
> Sydney NSW 2001  
> Australia  
> E-mail: removed\_email\_address@domain.invalid

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/6 "2009-11-21T18:20:03Z")

</div>

Marnen,

On 2009-11-22 03:58, Marnen Laibow-Koser wrote:

> Philip R. wrote:
> 
> > People,
> > 
> > For more than one white space char:
> > 
> > string.gsub( /^…:\s\*/, ‘’ )
> 
> Actually, \* is zero or more. For one or more, use + .

I wanted zero or more whitespaces so \* is correct.

Thanks,

Phil.

> > > > - I can do this with two gsubs - is it possible to do it with one?  
> > > > Sydney NSW 2001  
> > > > Australia  
> > > > E-mail: removed\_email\_address@domain.invalid
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

–  
Philip R.

GPO Box 3411  
Sydney NSW 2001  
Australia  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/7 "2009-11-21T18:00:44Z")

</div>

On 21.11.2009 16:18, Philip R. wrote:

> People,
> 
> For more than one white space char:
> 
> string.gsub( /^…:\s\*/, ‘’ )

Your original example had whitespace before the “To”. Don’t you want to  
get rid of that as well?

Btw, I’d rather name the word explicit, so for me it would be one of

s.sub /To:\s+/, ‘’  
s.sub /^(\s\*)To:\s\*/, ‘\1’

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 22, 2009, 11:42am UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/8 "2009-11-22T11:42:57Z")

</div>

On 21.11.2009 18:26, Philip R. wrote:

> > Btw, I’d rather name the word explicit, so for me it would be one of
> > 
> > s.sub /To:\s+/, ‘’  
> > s.sub /^(\s\*)To:\s\*/, ‘\1’
> 
> s.sub /^To:\s\*/, ‘\1’
> 
> is correct (I was indenting code to differentiate it) but why is  
> explicit better?

Because it avoids accidentally doing the substitution of other text  
which consists of two characters. Even if that text is not supposed to  
appear in your input, using the explicit form helps documenting what’s  
intended. And the code is more robust: it will not break if the input  
changes.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [November 21, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/removing-optional-white-space/179302/9 "2009-11-21T18:27:04Z")

</div>

Robert,

On 2009-11-22 04:00, Robert K. wrote:

> On 21.11.2009 16:18, Philip R. wrote:
> 
> > People,
> > 
> > For more than one white space char:
> > 
> > string.gsub( /^…:\s\*/, ‘’ )
> 
> Your original example had whitespace before the “To”. Don’t you want to  
> get rid of that as well?

Sorry, I was just indenting to differentiate the examples from the text.

> Btw, I’d rather name the word explicit, so for me it would be one of
> 
> s.sub /To:\s+/, ‘’  
> s.sub /^(\s\*)To:\s\*/, ‘\1’

s.sub /^To:\s\*/, ‘\1’

is correct (I was indenting code to differentiate it) but why is  
explicit better?

Thanks,

## Phil.

Philip R.

GPO Box 3411  
Sydney NSW 2001  
Australia  
E-mail: removed\_email\_address@domain.invalid
