[R] splitting a string based on the last underscore
Dimitri Liakhovitski
dimitri.liakhovitski at gmail.com
Fri Jul 29 18:59:16 CEST 2011
Thanks a lot, Jim!
Dimitri
On Fri, Jul 29, 2011 at 12:48 PM, jim holtman <jholtman at gmail.com> wrote:
> But your requirements said "last underscore"; so we now have a change
> that really says the first underscore. So we change the regex to:
>
>> strsplit(sub("[^_]+_(.+)_(.+).o$", "\\1 \\2", x), ' ')
> [[1]]
> [1] "a1" "2.5"
>
> [[2]]
> [1] "a2" "2.53"
>
> [[3]]
> [1] "a3_bla" "1"
>
>>
>
>
> On Fri, Jul 29, 2011 at 12:40 PM, Dimitri Liakhovitski
> <dimitri.liakhovitski at gmail.com> wrote:
>> Thank you, Jim.
>> It's close but not all the way. The desired result is:
>>> [[1]]
>>> [1] "a1" "2.5"
>>>
>>> [[2]]
>>> [1] "a2" "2.53"
>>>
>>> [[3]]
>>> [1] "a3_bla" "1"
>>
>> Dimitri
>>
>> On Fri, Jul 29, 2011 at 12:35 PM, jim holtman <jholtman at gmail.com> wrote:
>>> try this:
>>>
>>>> x<-c("name_a1_2.5.o","name_a2_2.53.o","name_a3_bla_1.o")
>>>> strsplit(sub(".*?([^_]+)_([^-]+).o$", "\\1 \\2", x), ' ')
>>> [[1]]
>>> [1] "a1" "2.5"
>>>
>>> [[2]]
>>> [1] "a2" "2.53"
>>>
>>> [[3]]
>>> [1] "bla" "1"
>>>
>>>>
>>>
>>>
>>> On Fri, Jul 29, 2011 at 12:08 PM, Dimitri Liakhovitski
>>> <dimitri.liakhovitski at gmail.com> wrote:
>>>> Hello!
>>>> Hope you could help me split the strings.
>>>>
>>>> I have a set of strings:
>>>>
>>>> x<-c("name_a1_2.5.o","name_a2_2.53.o","name_a3_bla_1.o")
>>>>
>>>> I need to extract from each string:
>>>>
>>>> 1. Its unique part that comes before the last "_", i.e.: "a1","a2","a3_bla".
>>>> 2. The part that comes after the last "_" and before ".o" at the end,
>>>> i.e.: 2.5, 2.53, 1.
>>>>
>>>> Thank you so much for your help!
>>>>
>>>> --
>>>> Dimitri Liakhovitski
>>>> marketfusionanalytics.com
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>>
>>>
>>> --
>>> Jim Holtman
>>> Data Munger Guru
>>>
>>> What is the problem that you are trying to solve?
>>>
>>
>>
>>
>> --
>> Dimitri Liakhovitski
>> marketfusionanalytics.com
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>
--
Dimitri Liakhovitski
marketfusionanalytics.com
More information about the R-help
mailing list