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
如何在R中提取具有特殊字符的模式之间的字符串_R_Regex_Gsub - Fatal编程技术网

如何在R中提取具有特殊字符的模式之间的字符串

如何在R中提取具有特殊字符的模式之间的字符串,r,regex,gsub,R,Regex,Gsub,从字符串a中,我想提取|Request |和下一个|之间的所有内容: a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed " gsub(".*\\|Request\\| (.+) |.*", "\\1", a) a您需要使用惰性点,并且您的输入模式应该与整个输入相匹配,因为您要替换为捕获组: a <- &

从字符串
a
中,我想提取
|Request |
和下一个
|
之间的所有内容:

 a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
    
 gsub(".*\\|Request\\| (.+) |.*", "\\1", a)

a您需要使用惰性点,并且您的输入模式应该与整个输入相匹配,因为您要替换为捕获组:

a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
sub("^.*\\|Request\\|\\s*(.+?)\\s*\\|.*$", "\\1", a)

[1] "Sample inlet port of the HIP cartridge with"

a您可以使用
sub
捕获
请求
之后的所有内容,直到下一个
发生

sub(".*\\|Request\\|(.*?)\\|.*", "\\1", a)
#[1] "\nSample inlet port of the HIP cartridge with "

您可以使用捕获组
\\\\\\\\\\\\\\\\\([^\]+)
stringr::stru匹配(a,“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\([^\\\\]+)”[,2]
获取值,如果之后不再出现|符号,我希望从第一次匹配到。我怎么能做到这一点呢?我没有足够的样本输入来提出一个足够好的替代正则表达式,甚至不值得在这里发表评论。