[R] split a string a keep the last part
jun.shen.ut at gmail.com
Thu Aug 28 20:18:12 CEST 2014
Thanks for everyone who replied with those wonderful solutions!
On Thu, Aug 28, 2014 at 2:11 PM, Enrico Schumann <es at enricoschumann.net>
> On Thu, 28 Aug 2014, Jun Shen <jun.shen.ut at gmail.com> writes:
> > Hi everyone,
> > I believe I am not the first one to have this problem but couldn't find a
> > relevant thread on the list.
> > Say I have a string (actually it is the whole column in a data frame) in
> > format like this:
> > test<- 'AF14-485-502-89-00235'
> > I would like to split the test string and keep the last part. I think I
> > do the following
> > sub('.*-.*-.*-.*-(.*)','\\1', test)
> > to keep the fifth part of the string. But this won't work if other
> > have more or fewer parts separated by '-'. Is there a general way to do
> > Thanks.
> > Jun
> This should work for your example:
> gsub(".*-([^-]*)$", "\\1", test)
> Enrico Schumann
> Lucerne, Switzerland
[[alternative HTML version deleted]]
More information about the R-help