How to clear input before calling regular expression?

I am making a Java program that parses user input using a regular expression. For example, if the user enters /me eats , he must match /me and replace it with <move> . However, Java does not match properly, since / is a special character for regular expressions. How to automatically replace all special Java regular expression characters with screens?

For instance:

  • /me becomes \/me
  • * becomes \*
  • [ becomes \[
  • etc.

before you put it in Pattern.compile .

This is not a command system. I allow users to specify how to indicate the movement of roles. If this helps, here is a mockup of how the user indicates what they consider to be a role-based movement:

A mockup of the preferences pane that would control this system

+6
source share
3 answers

Supuhstar, I believe this is the one liner you are looking for (see online demo ):

 String sanitized = subject.replaceAll("[-.\\+*?\\[^\\]$(){}=!<>|:\\\\]", "\\\\$0"); 

This adds a backslash to all of the following characters:

 . + * ? [ ^ ] $ ( ) { } = ! < > | : - \ 

Test input: String subject = ".+*?[^]$(){}=!<>|:-\\";

Conclusion: \.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:-\\

Further, as you like, you can continue:

 Pattern regex = Pattern.compile(sanitized); 

Notes:

  • Like Perl and PHP, Java also has syntax to avoid a whole line: you put it between \Q and \E This is what Pattern.quote does for you, but it quotes more text than you want for your situation.

  • This is just one possible solution that meets your specific requirement to add a backslash. For more options, also see. Does the Pattern.LITERAL template use the same thing as the Pattern.quote?

+12
source

Not sure if this will work, but what about:

 String inputString = .... inputString = Pattern.compile(inputString, Pattern.LITERAL).pattern(); //process inputString now 

Not currently verified. Will delete the answer if it is really not correct.

0
source

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); //cmd.getName() -> "me" //cmd.getArgs() -> ["eats"] } 

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(); //use the "action" string in whatever 3rd person context you desire. } 
-1
source

Source: https://habr.com/ru/post/969419/


All Articles