I’m struggling to understand why I seem unable to include a shorthand character class such as \\d or \\w inside a user-defined character class between [and ] (although I have seen cases where such an inclusion can be done). What I want to do in this illustrative example is relocate the currency symbol from the right end of the string to the start of the string:
a_1 <- c("155.88¥","5156.04€","656","1566.1$")
sub("([\\w.]+)([€$¥])", "\\2\\1", a_1) # doesn't work
sub("([\\d.]+)([€$¥])", "\\2\\1", a_1) # doesn't work
sub("([0-9.]+)([€$¥])", "\\2\\1", a_1) # works
Why does only the fully user-defined character class work but not those that involve the shorthand character classes?
Expected result:
[1] "¥155.88" "€5156.04" "656" "$1566.1"
>Solution :
As requested:
Character classes such as \\d, \\s, \\w are from Perl so when you use those make sure to add perl = T in your code.
For example:
sub("([\\w.]+)([€$¥])", "\\2\\1", a_1, perl = T)
More information can be found here:
https://perldoc.perl.org/perlrecharclass