grep大文本文件中大括号{}内的字符串

时间:2017-06-28 22:00:11

标签: r regex

我有一份手稿,我已将其转换为文本文件。它包含大括号中包含的许多不同长度的DOI

{doi: 10.1109/5.771073}

我想greis for the dois并将它们导出到另一个文本文件中。

我可以使用grep这样它只返回doi,而不是它所在的整个句子/向量吗?

1 个答案:

答案 0 :(得分:5)

Manuscript = "a lot of text that contains some {doi: 10.1109/5.771073}
some line may contain {doi: 1.2/3.4} and {doi: 5.6/7.8}
Of course other lines may contain nothing interesting"

library(stringr)
Temp = unlist(str_extract_all(Manuscript, "\\{doi:.*?\\}"))
AllDOIs = gsub("\\{doi:\\s*(.*)}", "\\1", Temp)
AllDOIs
[1] "10.1109/5.771073" "1.2/3.4"          "5.6/7.8"