我正在从 pdf 中提取行,并尝试使用 dplyr::filter(stringr::str_detect(my_column, 'my string'))
检测特定字符串。
字符串似乎没有可检测的编码。
这是 PDF 文件的链接:https://bioconductor.org/packages/release/bioc/vignettes/Rsubread/inst/doc/SubreadUsersGuide.pdf
该字符串是第 42 页表(左侧列)中的破折号。
我已尝试检测 em-dash 的几种表示形式,但无法在本文档中找到。
我如何确定这个 em-dash 的编码,以便我可以用它过滤我的 tibble?
pdftools::pdf_text("SubreadUsersGuide.pdf") %>%
stringr::str_split(pattern = '\r') %>%
tibble::tibble(
line = .
) %>%
tidyr::unnest(cols = line) %>%
dplyr::filter(
stringr::str_detect(line, pattern = '^EM_DASH')
)
最佳答案
你要匹配的字符不是破折号,而是MINUS
sign属于Symbol, Math Unicode类别,带有U+2212
编码。
要匹配字符串开头的任何一个或多个 Unicode 破折号 + 减号,您可以使用
pattern = "^[\\p{Pd}\\xAD\\u2212]+"
这里,
^
- 字符串的开始[
- 字符类的开始:\p{Pd}
- 任意 Puncutation, Dash字符\xAD
- 软连字符\u2212
- 减号。
]+
- 字符类的结尾,出现一次或多次。
参见 regex demo .
关于r - 为什么 stringr::str_detect 无法检测到我的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64230505/