Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 - Fatal编程技术网

R 仅提取正则表达式中模式的第二个实例

R 仅提取正则表达式中模式的第二个实例,r,regex,R,Regex,我试图使用编程语言R、版本4.0.2和stringr包中的正则表达式从字符串中提取模式的第二个实例 > test_string <- "Viscocity S <=0.25 S <=0.25 Levorotatory S <=21 R <=2.5 Giminal S

我试图使用编程语言
R
、版本4.0.2和
stringr
包中的正则表达式从字符串中提取模式的第二个实例

> test_string <- "Viscocity                      S   <=0.25        S   <=0.25 Levorotatory                      S      <=21        R      <=2.5 Giminal                    S      <=1        S      <=1"

>test_string您可以将下面的模式与
str_match
一起使用:

(?<=Levorotatory)(?:\s*([SRI]|N/I)\s*([^\w\s]*\d*\.?\d+)){2}
"S      <=21"
regex <- "(\\s*(?:S|R|I|N/I)(\\s*\\W*\\d*\\.?\\d?\\d?\\d?\\s*)){2}"
str_trim(str_extract_all(test_string, glue('(?<=Levorotatory){regex}')))
output: "S      <=21        R      <=2.5"
(?<=Levorotatory)(?:\s*([SRI]|N/I)\s*([^\w\s]*\d*\.?\d+)){2}
library(stringr)
pattern <- "(?<=Levorotatory)(?:\\s*([SRI]|N/I)\\s*([^\\w\\s]*\\d*\\.?\\d+)){2}";
x <- "Viscocity                      S   <=0.25        S   <=0.25 Levorotatory                      S      <=21        R      <=2.5 Giminal                    S      <=1        S      <=1"
results <- str_match(x, pattern)[,-1]
results
# => [1] "R"     "<=2.5"