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"