Solved regex read out of array

Discussion in 'Spigot Plugin Development' started by aufgosh, Mar 5, 2020.

  1. Hey!

    I am currently making an Admin-Gui. One of the functions should allow you to send a Title using the sendTitle() function to all players, now to my Question:

    How should can i exactly split my command arguments using regex if command is something like

    /test "Test Title hello" "yes this really is a test Title"

    The first content between "" should be the Headline and the second just the writing.
     
  2. drives_a_ford

    Moderator

    First of all, you'll want to use String#join to join the string back together as the onCommand method gives you an array of arguments.

    Now that you've got the whole string, the easiest solution would be to String#split the resulting string at [/icode]" "[/icode].
    Note that the 0th entry in the array will start with and the other will end with a ". You'll probably want to get rid of that.
    And be sure to check that you do in fact have the correct arguments.

    If you do wish do go the full regex way, you can use "(.*)" "(.*)" and get group 1 and 2.
     
  3. I am not sure hot to execute properly, i've joined the string but i am unsure on how to split at
    Code (Text):
    " "
     
  4. drives_a_ford

    Moderator

    Just execute the split method (that I linked to above) with the appropriate argument. Make sure to escape the quotations, though ("\" \"" is the string you want to split at).
     
    • Like Like x 1
  5. When joining arguments into a string, I like to use a StringBuilder. I’m not sure if this is what you’re looking for, but this is it:


    Code (Java):
    StringBuilder x = new StringBuilder;
    for (String arg : args) {
        x.append(arg).append(“ “);
    }
    player.sendMessage(x.toString)
     
  6. drives_a_ford

    Moderator

    Or, you know... String result = String.join(" ", args);
     
    • Like Like x 1