[R] Removing a space from a string

cpoiw@rt m@iii@g oii chemo@org@uk cpoiw@rt m@iii@g oii chemo@org@uk
Tue Jul 28 22:48:07 CEST 2020


This RegEx would do it I think:  \s(?=.*\s\d*\.)

Looks for space - \s
Before any strings followed by space, numbers, period

text <- "STR ING 01.  Remainder of the string"

stringr::str_replace_all(text, "\\s(?=.*\\s\\d*\\.)", "")

Should do it I think!

On 2020-07-28 21:34, Dennis Fisher wrote:
> It is possible that there will be > 1 space.  But, most likely only
> one (i.e., a solution for one space will suffice; a solution for > 1
> space would be even better)
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com [1]
> 
>> On Jul 28, 2020, at 1:29 PM, cpolwart using chemo.org.uk wrote:
>> 
>> On 2020-07-28 21:20, Dennis Fisher wrote:
>> 
>>> R 4.0.2
>>> OS X
>>> Colleagues
>>> I have strings that contain a space in an unexpected location.
>>> The
>>> intended string is:
>>> “STRING 01.  Remainder of the string"
>>> However, variants are:
>>> “STR ING 01.  Remainder of the string"
>>> “STRIN G 01.  Remainder of the string"
>>> I would like a general approach to deleting a space, but only if
>>> it
>>> appears before the period.  Any suggestions on a regular
>>> expression
>>> for this?
>> 
>> You aren't deleting the space before 0? Is that in the requirement?
> 
> 
> 
> Links:
> ------
> [1] http://www.plessthan.com/



More information about the R-help mailing list