Just use Pattern.html # quote (java.lang.String) :
public String quote(String input) { return Pattern.quote(input).substring(2, input.length() + 2); // quote() adds '\Q' and '\E', substring used to remove those. } String input = "/me eats"; String cmd = input.replaceAll(quote("/me"), /* replacement */);
So, I assume that this is a command system, in which case I would recommend not using a regular expression for your parsing.
This is usually much simpler with /command <arguments>... :
public class Command { private final String name; private final String[] args; public Command(String name, String... args) { this.name = name; this.args = args; } public String getName() { return this.name; } public String[] getArgs() { return this.args; } }
An example of this use:
String commandPrefix = "/"; String input = "/me eats"; if (input.startsWith(commandPrefix)) { String[] raw = input.split(" "); Command cmd = new Command(raw[0].substring(commandPrefix.length(), raw[0].length(), Arrays.copyOfRange(raw, 1, raw.length - 1);
Putting that aside, which determines how to execute the command, you can simply process the me command as follows:
public void onCommand(Command cmd) { StringBuilder sb = new StringBuilder(); boolean first = true; for (String s : cmd.getArgs()) { if (first) { first = false; } else { sb.append(" "); } sb.append(s); } String action = sb.toString();
Rogue source share