我正在使用 Zotero 從 PDF 創建一個 BibTeX 參考串列,它使用 { } 圍繞必須保留大小寫的單詞。
標題 = {巴西東南部 {Bokermannohyla} caramaschii ({Anura}: {Hylidae}) 的新繁殖棲息地、產卵微生境和父母照料},
但是,我團隊中的一些人使用 Mendeley,它似乎不知道 BibTeX 格式的這個規則,從我發送的 BibTeX 檔案匯入后,{ } 仍然出現在他們的標題中。
所以我想寫一個小腳本(在R中)來洗掉標題(和其他欄位)的主{}內的{},這樣在修改后的檔案中,上面的行就會變成如下所示。
標題 = {巴西東南部 Bokermannohyla caramaschii (Anura: Hylidae) 的新繁殖棲息地、產卵微生境和父母照料},
我已經嘗試了很多,但沒有任何效果。什么是正則運算式來做到這一點?
uj5u.com熱心網友回復:
您可以轉換正則運算式的匹配項
(?<!^title = ){|}(?!,$)
清空字串(使用perl=TRUE)。
演示
正則運算式可以分解如下。(我將空格顯示為包含空格的字符類,以便讀者可以看到它們。)
(?<! # begin a negative lookbehind
^ # match the start of the string
title[ ]=[ ] # match 'title = '
) # end negative lookbehind
{ # match '{'
| # or
} # match '}'
(?! # begin a negative lookahead
,$ # match a comma at the end of the string
) # end a negative lookahead
uj5u.com熱心網友回復:
如果我們可以確定“%%%”和“###”字串不會出現在標題中,那么這是一個有效的策略。首先,我們將第一個“{”更改為“%%%”,最后一個“}”更改為“###”。然后洗掉所有“{”和“}”,然后將第一個“{”和最后一個“}”放回原處。
txt <- "title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil}},"
txt2 <- sub("(^[^{] )(\\{)", "\\1%%%", txt) # placeholder for first "{"
txt3 <- sub("(\\})([^}]*$)", "###\\2", txt2) # " " for last "}"
txt4 <- gsub("\\{|\\}", "", txt3) # remove the rest
txt5 <- sub("%%%", "{", tx4) # put the leading and trailing ones back
txt6 <- sub("###", "}", txt5)
txt6
[1] "title = {Novel breeding habitat, oviposition microhabitat, and parental care in Bokermannohyla caramaschii (Anura: Hylidae) in southeastern Brazil},"
uj5u.com熱心網友回復:
這是一個決議器,它只洗掉{and },并且只有在完整的{ ... }. 它不會假裝快速或高效,但使用合理長度的字串,您應該不會注意到任何滯后。
func <- function(S) {
spl <- strsplit(S, "")[[1]]
out <- character(0)
inbrace <- 0L
for (i in seq_along(spl)) {
ch <- spl[i]
if (ch == "{") {
if (inbrace < 1L) out <- c(out, ch)
inbrace <- inbrace 1L
} else if (ch == "}") {
if (inbrace == 0L) {
stop("unmatched close brace at: ", i)
} else if (inbrace == 1L) {
out <- c(out, ch)
}
inbrace <- max(0L, inbrace - 1L)
} else out <- c(out, ch)
}
if (inbrace != 0L) stop("finished missing ", inbrace, " close-brace(s)")
paste(out, collapse = "")
}
演示:
func('title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil}},')
# [1] "title = {Novel breeding habitat, oviposition microhabitat, and parental care in Bokermannohyla caramaschii (Anura: Hylidae) in southeastern Brazil},"
它試圖非常具體,如果}發生不匹配或輸入結束而 a{仍然不匹配,則失敗。
func('title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil},')
# Error in func("title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil},") :
# finished missing 1 close-brace(s)
func('title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla}} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil}},')
# Error in func("title = {Novel breeding habitat, oviposition microhabitat, and parental care in {Bokermannohyla}} caramaschii ({Anura}: {Hylidae}) in southeastern {Brazil}},") :
# unmatched close brace at: 156
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466803.html
上一篇:如何改進我的正則運算式決議模式?
