我们可以调用sub()
来匹配整个字符串,从(1)任意数量的任何字符开始,然后(2)一个字面点,然后(3)使用捕获组来捕获以下字符,然后(4 )字面意思_tot
。然后,我们可以使用\1
反向引用原子(根据R的字符串编码规则将反斜杠正确反斜杠转义)用捕获的字符替换整个字符串。
DF$c <- sub('^.*\\.(.)_tot$','\\1',DF$a);
DF;
## a b c
## 1 a.b.c_tot 1 c
## 2 b.c.d_tot 2 d
## 3 d.e.g_tot 3 g
是的,我看到的问题;如果DF$a
包含的值与预期模式不匹配,则sub()
调用会将它们传递到新的DF$c
列。下面是一个使用Perl branch reset特征的哈克溶液:
DF <- data.frame(a=c('a.b.c_tot','b.c.d_tot','d.e.g_tot','non-matching'),b=c(1L,2L,3L,4L),stringsAsFactors=F);
DF$c <- sub(perl=T,'(?|^.*\\.(.)_tot$|^.*$())','\\1',DF$a);
DF;
## a b c
## 1 a.b.c_tot 1 c
## 2 b.c.d_tot 2 d
## 3 d.e.g_tot 3 g
## 4 non-matching 4
这里的一个更好的解决方案,涉及存储该正则表达式中的变量提前,并使用grepl()
和replace()
以取代NA不匹配的值调用sub()
之前:
re <- '^.*\\.(.)_tot$';
DF$c <- sub(re,'\\1',replace(DF$a,!grepl(re,DF$a),NA));
DF;
## a b c
## 1 a.b.c_tot 1 c
## 2 b.c.d_tot 2 d
## 3 d.e.g_tot 3 g
## 4 non-matching 4 <NA>
您可以使用'子()'函数,如果要提取那封信。 –
谢谢,substring()完成了这项工作!看起来好像比分:) :) – HoHoHo