Removing optional white space

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 Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

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 Rhoades wrote:

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 Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

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

···

On Saturday 21 November 2009, Philip Rhoades wrote:

>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.
>

People,

For more than one white space char:

  string.gsub( /^..:\s*/, '' )

Phil.

···

On 2009-11-22 02:04, Philip Rhoades wrote:

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 Rhoades wrote:

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 Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

Philip Rhoades wrote:

People,

For more than one white space char:

  string.gsub( /^..:\s*/, '' )

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

Phil.

into:

phil

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

Thanks,

Phil.

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

Best,

···

On 2009-11-22 02:04, Philip Rhoades wrote:

On 2009-11-22 01:54, Philip Rhoades wrote:

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

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

···

On 21.11.2009 16:18, Philip Rhoades wrote:

People,

For more than one white space char:

  string.gsub( /^..:\s*/, '' )

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Marnen,

Philip Rhoades 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.

···

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

Phil.

On 2009-11-22 02:04, Philip Rhoades wrote:

On 2009-11-22 01:54, Philip Rhoades wrote:

into:

phil

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

Thanks,

Phil.

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

Robert,

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.

···

On 2009-11-22 04:00, Robert Klemme wrote:

On 21.11.2009 16:18, Philip Rhoades wrote:

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

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

···

On 21.11.2009 18:26, Philip Rhoades 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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/