Why is stringr::str_detect not able to detect my string?

Viewed 853

I am extracting lines from a pdf, and trying to detect a specific string with dplyr::filter(stringr::str_detect(my_column, 'my string')).

The string does not seem to have an encoding that is detectable.

Here is a link to the PDF file: https://bioconductor.org/packages/release/bioc/vignettes/Rsubread/inst/doc/SubreadUsersGuide.pdf

The string is the em-dash in the table (left hand column) on page 42.

I have tried detecting several representations of the em-dash, but cannot find the one in this document.

How can I determine the encoding of this em-dash so that I can filter my tibble with it?

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')
  )
1 Answers

The character you want to match is not a dash, it is a MINUS sign belonging to the Symbol, Math Unicode category, with U+2212 code.

To match any one or more Unicode dashes + MINUS signs at the start of a string, you may use

pattern = "^[\\p{Pd}\\xAD\\u2212]+"

Here,

  • ^ - start of string
  • [ - start of a character class:
  • ]+ - end of a character class, one or more occurrences.

See the regex demo.

Related