Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex R:gsub空格之间的单词_Regex_R_Gsub - Fatal编程技术网

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.