4

I am working on a command based feature for a project in Java and am having trouble when introducing arguments to these commands.

For example all the commands are stored like this this:

"Hey tell [USER] to [ACTION]"

Now when the user submits their command it will look like this:

"Hey tell Player to come see me"

Now I need to know how I can compare the users inputted command to the stored command containing placeholder values. I need to be able to compare the two strings and recognise that they are the same command and then from this extract the data [USER] and [ACTION] and return them as an array

array[0] = "Player"
array[1] = "come see me"

Really hope somebody can help me out, thanks

John Brown
  • 43
  • 3
  • 2
    Could you clarify what is the problem? You can make an array from a sentence like this by using string.split(" ") and compare two Strings using string1.equals(string2). – TomekK Jun 22 '16 at 08:54
  • do you tryed to use concatination "Hey tell" + [USER] + "to" + "[ACTION]" – Youcef LAIDANI Jun 22 '16 at 08:55
  • Well basically I have an arraylist which contains the commands with the placeholder values. Such as "Hey tell [USER] to [ACTION]". Now when the user submits a command such as "Hey tell John to come see me" I need be able to compare that to the stored command in the arraylist, recognise it is the same command, and the extract the data from the command "John" and "Come see me". – John Brown Jun 22 '16 at 09:09

3 Answers3

1

You can use Pattern Matching as below:

    String command = "Hey tell [USER] to [ACTION]";
    String input = "Hey tell Player to come see me";
    String[] userInputArray = new String[2];

    String patternTemplate = command.replace("[USER]", "(.*)"); 
    patternTemplate = patternTemplate.replace("[ACTION]", "(.*)");

    Pattern pattern = Pattern.compile(patternTemplate);
    Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            userInputArray[0] = matcher.group(1);
            userInputArray[1] = matcher.group(2);

        } 
0

In case you do not need a stored String like "Hey tell [USER] to [ACTION]" and you can use Java (java.util.regex) Pattern and Matcher.

This is an example:

Pattern p = Pattern.compile("Hey tell ([a-zA-z]+) to (.+)");
List<Pattern> listOfCommandPattern = new ArrayList<>();
listOfCommandPattern.add(p);

Example, parse the command:

String user;
String command;
Matcher m;
// for every command
for(Pattern p : listOfCommandPattern){
   m = p.matcher(inputCommand);
   if (m.matches()) {
       user = m.group(1);
       command = m.group(2);
       break; // found user and command
   }
}
YKay
  • 131
  • 5
0

Here is a slightly more general version:

String pattern = "Hey tell [USER] to [ACTION]";
String line = "Hey tell Player to come see me";

/* a regular expression matching bracket expressions */
java.util.regex.Pattern bracket_regexp = Pattern.compile("\\[[^]]*\\]");

/* how many bracket expressions are in "pattern"? */
int count = bracket_regexp.split(" " + pattern + " ").length - 1;

/* allocate a result array big enough */
String[] result = new String[count];

/* convert "pattern" into a regular expression */
String regex_pattern = bracket_regexp.matcher(pattern).replaceAll("(.*)");
java.util.regex.Pattern line_regex = Pattern.compile(regex_pattern);

/* match "line" */
if (line_regex.matcher(line).matches()) {
    /* extract the matched strings */
    for (int i=0; i<count; ++i) {
        result[i] = line_matcher.group(i+1);
        System.out.println(result[i]);
    }
} else {
    System.out.println("Doesn't match.");
}
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263