Regex R:gsub空格之间的单词
我有一个文本如下所示:Regex R:gsub空格之间的单词,regex,r,gsub,Regex,R,Gsub,我有一个文本如下所示: a <- "233,236,241 solitude ΔE=1.9" 我尝试了两种方法: a1 <- strsplit(a,' ',fixed=TRUE)[[1]][2] a2 <- sapply(strsplit(a, " ", fixed=TRUE), "[", 2) 正确的方法是什么?试试这个: gsub("\\s.+$","",gsub("^.+[[:digit:]]\\s","",a)) 这里有一种使用捕获类(括号内的模式)和字符类(方括
a <- "233,236,241 solitude ΔE=1.9"
我尝试了两种方法:
a1 <- strsplit(a,' ',fixed=TRUE)[[1]][2]
a2 <- sapply(strsplit(a, " ", fixed=TRUE), "[", 2)
正确的方法是什么?试试这个:
gsub("\\s.+$","",gsub("^.+[[:digit:]]\\s","",a))
这里有一种使用捕获类(括号内的模式)和字符类(方括号内的模式)的方法 注释第一个捕获类模式:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\finds first space
\ an arbitrary number of times
\\ inside a character class an '^' as the first character ...
signals negation of character class. This one with only the space character in it.
\----- '^' marks the beginning of a character value
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ an arbitrary number of times
\\negation of character class with only the space character in it.
第二类捕获模式:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\finds first space
\ an arbitrary number of times
\\ inside a character class an '^' as the first character ...
signals negation of character class. This one with only the space character in it.
\----- '^' marks the beginning of a character value
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ an arbitrary number of times
\\negation of character class with only the space character in it.
第三类:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ the second space
\\anything after second space to end.
replacement
中的“\\\”
条目引用捕获类在模式中出现的顺序匹配。strsplit(a,,)[[1]][2]
适用于aOr,或者可能只是sub(.*\\s+(.\\\s+.*,“\\1”,a)
我得到“孤独”对于你的每一种方法……两个gsub
调用有什么意义?第一个将文本剪切到“孤独”之前的空格,第二个将文本从“孤独”之后的空格剪切到结尾。
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ the second space
\\anything after second space to end.