Removing optional white space

P

Philip Rhoades

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: (e-mail address removed)
 
P

Philip Rhoades

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.


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: (e-mail address removed)
 
S

Stefano Crocco

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

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
 
P

Philip Rhoades

People,

For more than one white space char:

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

Phil.


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.

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
R

Robert Klemme

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
 
M

Marnen Laibow-Koser

Philip said:
People,

For more than one white space char:

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

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




--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)

Best,
 
P

Philip Rhoades

Marnen,


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


I wanted zero or more whitespaces so * is correct.

Thanks,

Phil.


--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
P

Philip Rhoades

Robert,


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 Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top