# R 子集只有一个或两个字符 BEFORE 分开?
# R subset only one or two characters BEFORE pattern separately?
我想使用 rematches 和 gregexpr 或 sub 对列中模式前的一两个词进行子集化。
state<-regmatches(data[,2], gregexpr(??? , data[,2])) # California
type<-regmatches(data[,2], gregexpr( ??? , data[,2])) # Valencia
这是例句,之前的词单独提取失败
sample<-" 2018 summer California Valencia orange"
sub("[O|o]range.*","",sample)
[1] " 2018 summer California Valencia "
如何将模式前的一个字符和模式前的前两个字符子集化?
我们可以使用正则表达式查找来提取 space (\s*
) 和 'orange' 子串
之前的单词 (\w+
)
regmatches(sample, regexpr("(\w+)(?=\s*[Oo]range)", sample, perl = TRUE))
#[1] "Valencia"
在第二种情况下,更改环视以在 'orange'
之前包含一个词和 space
regmatches(sample, regexpr("(\w+)(?=\s*\w+\s*[Oo]range)", sample, perl = TRUE))
#[1] "California"
我想使用 rematches 和 gregexpr 或 sub 对列中模式前的一两个词进行子集化。
state<-regmatches(data[,2], gregexpr(??? , data[,2])) # California
type<-regmatches(data[,2], gregexpr( ??? , data[,2])) # Valencia
这是例句,之前的词单独提取失败
sample<-" 2018 summer California Valencia orange"
sub("[O|o]range.*","",sample)
[1] " 2018 summer California Valencia "
如何将模式前的一个字符和模式前的前两个字符子集化?
我们可以使用正则表达式查找来提取 space (\s*
) 和 'orange' 子串
\w+
)
regmatches(sample, regexpr("(\w+)(?=\s*[Oo]range)", sample, perl = TRUE))
#[1] "Valencia"
在第二种情况下,更改环视以在 'orange'
之前包含一个词和 spaceregmatches(sample, regexpr("(\w+)(?=\s*\w+\s*[Oo]range)", sample, perl = TRUE))
#[1] "California"