Re: [R] extracting characters from string

2011-02-11 Thread Soumendra
Well, I believe, given the original statement of the problem, that it is philosophically wrong to use the gsub approach. What if there are 50 underscores instead of 5, and you want to extract the characters after the 23rd underscore? By using gsub, you are trying to fight against the pattern of und

Re: [R] extracting characters from string

2011-02-10 Thread jim holtman
A safer way to make sure you don't match the underscore: > gsub("[^_]*_[^_]*_([^_]*).*", "\\1", "abcd_efgh_X_12ab3_dfsfd") [1] "X" On Thu, Feb 10, 2011 at 2:06 PM, Henrique Dallazuanna wrote: > So, a way could be: > > gsub("(.*)_(.*)_(.*)_.*", "\\3",  "abcd_efgh_X_12ab3_dfsfd") > >

Re: [R] extracting characters from string

2011-02-10 Thread Henrique Dallazuanna
So, a way could be: gsub("(.*)_(.*)_(.*)_.*", "\\3", "abcd_efgh_X_12ab3_dfsfd") On Thu, Feb 10, 2011 at 3:47 PM, Soumendra wrote: > Hi Henrique, > > I believe your solution is wrong as it is fitted to find 12ab3, > whereas Yan seems to be asking for the characters after the second > unders

Re: [R] extracting characters from string

2011-02-10 Thread Soumendra
Hi Henrique, I believe your solution is wrong as it is fitted to find 12ab3, whereas Yan seems to be asking for the characters after the second underscore and before the third underscore. For example, gsub(".*_.*_(.*)_.*", "\\1", "abcd_efgh_X_12ab3_dfsfd") would still yield 12ab3 even though,

Re: [R] extracting characters from string

2011-02-10 Thread Henrique Dallazuanna
Try this: gsub(".*_.*_(.*)_.*", "\\1", "abcd_efgh_12ab3_dfsfd") On Thu, Feb 10, 2011 at 9:42 AM, Yan Jiao wrote: > Dear R gurus, > > > > If I got a vector with string characters like "abcd_efgh_12ab3_dfsfd", > how could I extract "12ab3", which is the characters after second > underscore and be

[R] extracting characters from string

2011-02-10 Thread Yan Jiao
Dear R gurus, If I got a vector with string characters like "abcd_efgh_12ab3_dfsfd", how could I extract "12ab3", which is the characters after second underscore and before the third underscore? Tons of thanks yan *