Well as you already know java uses \ for escape sequences. If you want to use \ in a String when the escape sequence is not legal, you use \\ instead of \. So in regex when you want to use \d, you'll have to escape \ so you'll write \\d. By doing this you'll pass the compilation and the regex engine will process it as \d...
One notable exception to the rule of using \\ in the regex pattern is if you pass the regex pattern as a command line argument. If you do so, you simply use \ and not \\. I learnt this the hard way. Here is my made up example code:
Here is how you could run the code after compilation. If you see below, I am just using \ and not \\ and it still works. But this is the only exception to the rule i.e. if you use the regex from the command line. If you hard code the regex inside the code itself, then you need to use \\.
java Test "\w+"
java Test "\d"
But apparently iit also works if you use \\ instead of \ like shown below:
java Test "\\w+"
java Test "\\d"
Trying writing your own code and experimenting with it! Good luck.