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

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

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: [email protected]

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

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: [email protected]

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: [email protected]

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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: [email protected]

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]


Philip R.

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

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

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

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: [email protected]