I'm making a Java program that parses the user's input with a regex. For instance, if the user inputs /me eats, it should match the /me and replace it with <move>. However, Java isn't properly matching because / is a special character to regexes. How do I automatically replace all the various special Java regex characters with escapes?
For instance:
/me becomes \/me
* becomes \*
[ becomes \[
before it's put into Pattern.compile.
This is not a command system. I am allowing users to specify how to denote a roleplaying move. If it helps, here is a mockup of how the user specifies what they consider a roleplay move:

Supuhstar, I believe this is the one-liner you're looking for (see online demo):
String sanitized = subject.replaceAll("[-.\\+*?\\[^\\]$(){}=!<>|:\\\\]", "\\\\$0");
This adds a backslash to all of the following characters:
. + * ? [ ^ ] $ ( ) { } = ! < > | : - \
Test Input: String subject = ".+*?[^]$(){}=!<>|:-\\";
Output: \.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:-\\
Next, as you wanted, you can proceed with:
Pattern regex = Pattern.compile(sanitized);
Notes:
Like Perl and PHP, Java also has a syntax to escape an entire string: you place it between \Q and \E. That is what Pattern.quote does for you, but it quotes more text than you want for your situation.
This is only one possible solution answering your specific requirement of adding a backslash. For more options, also see Does using Pattern.LITERAL mean the same as Pattern.quote?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With