Error 'invalid regular expression '[:alpha:]+'' after migrating R and RStudio


I am having troubles running my code which was written under RStudio 1.3.959 after migrating to a new PC and installing RStudio 1.4.1717. The same error appears when running the code via base R (4.1.0). When using base R functions (grep, gregexpr, e.g. gregexpr("[:alpha:]+", "1234a")), there is no error message.



data_files <- 

data_files <- data_files %>%
  mutate(temp = data_files[,1]) %>%
           c("temp", "Trash"),
           sep = "\\.") %>%
  select(-"Trash") %>%
           c("run", "Trash"),
           sep = "[:alpha:]+", 
           remove = FALSE) %>%
  select(-"Trash") %>%
           c("Trash", "letters"),
           sep = "[:digit:]+") %>%
  select(-"Trash") %>%
  select("run", "letters") 

my data_folder contains csv files with name pattern (date-increment-letter.csv, e.g. 21021202a.csv)

Error message:

Error in gregexpr(pattern, x, perl = TRUE) : 
  invalid regular expression '[:alpha:]+'
In addition: Warning message:
In gregexpr(pattern, x, perl = TRUE) : PCRE pattern compilation error
	'POSIX named classes are supported only within a class'
	at '[:alpha:]+'

Could you point me what is missing in my fresh installation, please?

Thank you in advance!


I Googled the error and read this:

POSIX named classes are supported only within a class. A POSIX named class (ex: "[:digit:]") can only appear within a character class ("[[:digit:]]").

So from this example I tried to add the extra brackets and it seems to work now:


myData = data.frame(x = 1:2, 
                    y = c("texta5textb", "textc123textd"))

myData %>% separate(y, into =  c("a", "b"), "[[:digit:]]+")
#>   x     a     b
#> 1 1 texta textb
#> 2 2 textc textd

Created on 2021-07-20 by the reprex package (v2.0.0)

I'm not familiar with this style of regex, as I would use \d+ instead, but it seems to have solved the issue...

Hope this helps,

Yes, that was it - double square brackets. Thanks a lot! I don't know why it (single square brackets) worked with my old installation of R/RStudio...

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.