[R] strsplit and regex

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 16 00:33:46 CEST 2008


It's no harder: use sub() which is vectorized.  You've not actually 
defined the pattern, but here is my guess at your intentions:

x <- c("23:10:34", "23:20:30")
sub("([0-9][0-9]:)([0-9][0-9])(:[0-9][0-9])", "\\2", x)

Another interpetation would be

sub("([^:]*:)([^:]*)(:[^:]*)", "\\2", x)

for <something>:<desired>:<something else>

E.g. is 9:13:3 valid or not?


On Thu, 16 Oct 2008, Redding, Matthew wrote:

>
> Hi All,
>
> Just to make that question a bit harder - how
> do I apply that string extraction to vector of these time strings?
>
> Thanks,
>
> Matt Redding
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Redding, Matthew
>> Sent: Thursday, 16 October 2008 7:54 AM
>> To: r-help at r-project.org
>> Subject: [R] strsplit and regex
>>
>> Hi All,
>>
>> Is there a means to extract the "10" from "23:10:34" in one
>> pass using strsplit (or something else)?
>> tst <- "23:10:34"
>>
>> For example my attempt
>> strsplit(as.character(tst),"^[0-9]*:")
>> gives
>> [[1]]
>> [1] ""   ""   "34"
>>
>> Obviously it is matching the first two instances of [0-9].
>> Note that there may be only one digit before the first ":".
>>
>> How do I anchor the match to the begginning or better still,
>> just extract the number I want in one pass?
>>
>> I can see that I can add "begin" to the beginning of the
>> string, and match that and do something similar at the end,
>> getting rid of empty strings etc - but I think it would take
>> about 3 passess  - and the files are large. And besides that
>> code would be unlovely.
>>
>> Kind regards,
>>
>>
>> Matt Redding
>> ********************************DISCLAIMER**************...{{dr
>> opped:15...{{dropped:19}}
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list