#regex issue

1 messages Β· Page 1 of 1 (latest)

fiery bough
#

does anyone know why my regex string is failing? its supposed to detect if a string contains any of <, >, [ or ], it works on regextester but doesn't work ingame

[<>\\[\\]]
\\ = Escape character

System.out.println("[map]".matches("[<>\\[\\]]")) = false vs
https://gyazo.com/407b7f34996dde9b3d99619cf8c3357

fleet forge
#

heyho

#

the gyazo link is broken

fiery bough
fleet forge
#

oh yeah

#

String.matches will try to match the WHOLE string

fiery bough
#

ohhhhh

fleet forge
#

as you can see on regex101, it only matches the brackets, but not the whole string

#

that's why it returns false

#

if you wanna extract the brackets, you have to use a Pattern

#

something like this:

#
Pattern pattern = Pattern.compile("[<>\\[\\]]");
Matcher matcher = pattern.matcher("[map]");
fiery bough
#

oh, I see, so pattern is string.match but not whole string? or is it a configurable type thing using a different matcher

fleet forge
#

maybe explain what you are trying to achieve πŸ™‚

#

otherwise this is an XY problem lol

fiery bough
#

basically im creating a method that displays a command using a basecomponent, and when the message is clicked, it removes the [] and <> boxed items because they're not actual arguments, they're placeholders into a clickevent (suggest)

fleet forge
#

ah alright

#

so you want to do something like this:

#

[text] -> text
<text> -> text

#

right?

fiery bough
#

./world read <worldname> [config] -> /world read

fleet forge
#

ah okay so you wanna replace everything inside <> or [] brackets with "nothing"

#

right?

fiery bough
#

yeah, currently got this, seems to work using a maven test

String commandPlaintext;
StringBuilder commandPlaintextBuilder = new StringBuilder();
for (String element : commandText.split(" ")) {
    if (!Pattern.compile("[<>\\[\\]]").matcher(element).find()) continue;
    commandPlaintextBuilder.append(element).append(" ");
}
commandPlaintext = commandPlaintextBuilder.toString().trim();
#

thanks!

fleet forge
#

np!

#

btw

#

You can also use replaceAll

#
        String string = "/world create [arg1] <arg2>";
        System.out.println(string.replaceAll("<.*>","").replaceAll("\\[.*]",""));
#

this will simply print "/world create " (including the two spaces at the end)

fiery bough
#

oh interesting, thank you, didn't know replaceall could take regex πŸ€”

fleet forge
#

yeah it's a bit weird

#

String.replace takes a string to replace

#

String.replaceAll takes a regex

#

String.replaceFirst takes a regex too

fiery bough
#

:D?! java

fleet forge
#

but it's actually the same. String.replaceAll also uses a Pattern and Matcher internally πŸ˜„

#

this is the code for String.replaceAll:

    public String replaceAll(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }
fiery bough
#

interesting, why wouldnt they make replace regex too πŸ€”

fleet forge
#

well

#

replace is basically the same as replaceAll

#

just without regex support

#

I guess they added replace, then later they wanted to support regex so they added replaceFirst and replaceAll, i don't know πŸ˜„

fiery bough
#

I see, alright, well thank you for your help πŸ‘

fleet forge
#

np, have a nice day :3