Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数据框:
result <- c('Ab1 : 256 ug/mL(R), Ab2(disk); 18mm(S)', 'Ab1 : 4 ug/mL(S), Ab2(disk); <2mm(R)') df <- data.frame(result)
如果我想检查 'antibiotics1' 之后是否出现 '(R)' 应该怎么做?
grep("Ab1[[:print:]]*\\(R\\)", result)
给
[1] 1 2
而我想要的结果是
[1] 1
尝试这个:
grep("Ab1[^(]*?\\(R\\)", result) [1] 1 Ab1 match 'Ab1' literally [^(]*? match anything besides an opening parenthesis, non greedily (R) match '(R)' literally
在第二种情况下,如果不首先消耗至少一个左括号,就不可能进行这种匹配,因此只有第一次匹配。