Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem using \\d inside a user-defined character class

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading