0

I want to keep a string that is only letters, numbers or underscore. However when I use

gsub('[^A-z0-9_]',"","some lab value (x10^9L)")

I get

[1] "somelabvaluex10^9L"

Which is unexpected since ^ character is kept in the string. I tried different options without any luck.

Wietze314
  • 5,942
  • 2
  • 21
  • 40
  • 3
    `[A-z]` matches `[`, ``\``, `]`, `^`, `_`, `\``, and the English letters so the result **is** expected. You wanted to use `[^A-Za-z0-9_]` or just `\W`: `gsub('\\W+','','some lab value (x10^9L)')`, or `gsub('\\W+','','some lab value (x10^9L)', perl=TRUE)` or `gsub('[^_[:alnum:]]+','','some lab value (x10^9L)')` – Wiktor Stribiżew Sep 10 '19 at 11:19
  • 2
    So `[^A-Za-z0-9_]` should work. – s_baldur Sep 10 '19 at 11:20
  • Thanks you very much – Wietze314 Sep 10 '19 at 11:29
  • And I didn't know this `^` is called caret. Otherwise I would have found the answer... – Wietze314 Sep 10 '19 at 11:38

0 Answers0