[R] Regex to remove last character

Martin Morgan mtmorgan at fhcrc.org
Tue Jan 4 08:05:20 CET 2011


On 01/03/2011 02:44 PM, David Winsemius wrote:
> 
> On Jan 3, 2011, at 2:24 PM, rivercode wrote:
> 
>>
>> Hi,
>>
>> Have been having trouble trying to figure out the right regex
>> parameters to
>> remove the last "." in timestamp with the following format:
>>
>> Convert 09:30:00.377.853 to  09:30:00.377853
> 
> gsub("()(\\.)(.{3}$)", "\\1\\3" , "09:30:00.377.853")
> [1] "09:30:00.377853"

The 'g' in 'gsub' says 'make multiple substitutions in a single
character element' (compare gsub("0", "o", "09:30:00.377.853") with
sub("0", "o", "09:30:00.377.853")) whereas here there's just a single
substitution per character string ('.' with ''). Maybe

  sub("\\.([[:digit:]]+)$", "\\1", "09:30:00.377.853")

where the pattern is 'a literal period \\. followed by 1 or more digits
[[:digits:]]+ followed by an end-of-record $', with the 1 or more digits
remembered as \\1 for the replacement.

The [:digit:] is explained in ?regex; a less thorough solution would use
"\\.(\\d+)$".

Martin

> 
>>
>> -- 
>> View this message in context:
>> http://r.789695.n4.nabble.com/Regex-to-remove-last-character-tp3172466p3172466.html
>>
> 
> ==
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list