2017-06-16 15 views
8

मैं आर देव पर बहुत विशेष परिस्थितियों में पर्ल अभिव्यक्ति \\L\\1 साथ समस्या आ रही (2017/06/06 और 2017/06/16 r72796 बनाता है):क्या रेगेक्स \ एल 3.5.0 में समर्थित है?

bib <- readLines("https://raw.githubusercontent.com/HughParsonage/TeXCheckR/master/tests/testthat/lint_bib_in.bib", encoding = "UTF-8") 

leading_spaces <- 2 

is_field <- grepl("=", bib, fixed = TRUE) 
field_width <- nchar(trimws(gsub("[=].*$", "", bib, perl = TRUE))) 

widest_field <- max(field_width[is_field]) 

out <- bib 

# Vectorized gsub: 
for (line in seq_along(bib)){ 
    # Replace every field line with 
    # two spaces + field name + spaces required for widest field + space 
    if (is_field[line]){ 
    spaces_req <- widest_field - field_width[line] 
    out[line] <- 
     gsub("^\\s*(\\w+)\\s*[=]\\s*\\{", 
      paste0(paste0(rep(" ", leading_spaces), collapse = ""), 
        "\\L\\1", 
        paste0(rep(" ", spaces_req), collapse = ""), 
        " = {"), 
      bib[line], 
      perl = TRUE) 
    } 
} 

# Add commas: 
out[is_field] <- gsub("\\}$", "\\},", out[is_field], perl = TRUE) 

out[9] 
#> R-dev " author" 
#> R 3.4.0 " author  = {Tony Wood and Amélie Hunter and Michael O'Toole and Prasana Venkataraman and Lucy Carter}," 

पुन: पेश करने के लिए, यह आवश्यक है:

  • readLines फ़ाइल से, और एन्कोडिंग निर्दिष्ट करें। (dput का पुन: उत्पन्न नहीं होगा)
  • पेर्ल रेगेक्स में \\L या \\U का उपयोग करने के लिए।
  • पात्रों
  • का एक वेक्टर का उपयोग करने के लिए कि वेक्टर का एक तत्व कि UTF-8 (ऊपर में एमेलि में é) की आवश्यकता है के लिए

इस आर 3.5.0 में एक परिवर्तन है, या है मैं इस उदाहरण में \\L का दुरुपयोग कर रहा था?

+0

देखो, तो आपको सचेत कर दिया गया है: [* यह संभावना शामिल कीड़े, इसलिए सावधान अगर आप इसका इस्तेमाल हो सकता है *।] (Https://cran.r-project.org /bin/windows/base/rdevel.html)। –

+0

मैं स्निपेट नहीं बना सका - 'अग्रणी_ स्पेस' क्या है? –

+0

यह विशेष बग एक पैकेज के लिए आर सीएमडी चेक पर एक त्रुटि उत्पन्न कर रहा है। Nonreprex के बारे में खेद है, मैंने संपादित किया है। – Hugh

उत्तर

9

स्पष्ट रूप से कुछ अप्रत्याशित व्यवहार है।

जब \1 की चर्चा करते हुए, यह outputting काम करता है:

[1] " author  = {Tony Wood and Amélie Hunter and Michael O'Toole and Prasana Venkataraman and Lucy Carter}," 

बहरहाल, जब भी एक \U या \L\1 साथ प्रयोग किया जाता है, दूसरा backreference निकाल दिया जाएगा।

  • "\\U\\1": [1] " AUTHOR"
  • "\\U\\1\\E\\2": [1] " AUTHOR"

एक gsubfn समाधान अभी भी (यहाँ, toupper() साथ एक उदाहरण) काम करता है:

library(gsubfn) 
bib <- readLines("https://raw.githubusercontent.com/HughParsonage/TeXCheckR/master/tests/testthat/lint_bib_in.bib", encoding = "UTF-8") 
leading_spaces <- 2 
is_field <- grepl("=", bib, fixed = TRUE) 
field_width <- nchar(trimws(gsub("[=].*$", "", bib, perl = TRUE))) 
widest_field <- max(field_width[is_field]) 
out <- bib 

# Vectorized gsub: 
for (line in seq_along(bib)){ 
    # Replace every field line with 
    # two spaces + field name + spaces required for widest field + space 
    if (is_field[line]){ 
    spaces_req <- widest_field - field_width[line] 
    out[line] <- 
     gsubfn("^\\s*(\\w+)\\s*=\\s*\\{", 
      function(y) paste0(
        paste0(rep(" ", leading_spaces), collapse = ""), 
        toupper(y), 
        paste0(rep(" ", spaces_req), collapse = ""), 
        " = {" 
      ), 
      bib[line], engine="R" 
    ) 
    } 
} 
# Add commas: 
out[is_field] <- gsub("\\}$", "},", out[is_field], perl = TRUE) 

out[9] 

आउटपुट:

[1] " AUTHOR  = {Tony Wood and Amélie Hunter and Michael O'Toole and Prasana Venkataraman and Lucy Carter}," 

मेरे sessionInfo विवरण:

> sessionInfo() 
R Under development (unstable) (2017-06-19 r72808) 
Platform: i386-w64-mingw32/i386 (32-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

Matrix products: default 

locale: 
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] gsubfn_0.6-6 proto_1.0.0 

loaded via a namespace (and not attached): 
[1] compiler_3.5.0 tools_3.5.0 tcltk_3.5.0 
संबंधित मुद्दे