#How to split in java by "][" ?

15 messages · Page 1 of 1 (latest)

torn lintel
#

I'm trying to split a string , but I get 'java.util.regex.PatternSyntaxException' exception because its seeing "][" as Unclosed character class. Anybody knows the escape character for this?

String pattern = "]".concat"["
String toSplit = "|][%";
List<String> splitted = Arrays.asList(toSplit.split(pattern))

I also tried pattern with value "\]\[" but still doens't work. Any suggestions?

fossil acornBOT
#

This post has been reserved for your question.

Hey @torn lintel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

chrome vortex
#

"\\]\\["

torn lintel
#

It doesn't work, this will be seen as "][" and will not be contained in toSplit

spring templeBOT
#

razvanr
Compile Error! Click the errors reaction for more information.
(You may edit your message to recompile.)

reef urchin
#

you're not even using toSplit

#

/run

System.out.println(java.util.Arrays.toString("|][%".split("\\]\\[")));
lost canopyBOT
#

Here is your java(15.0.2) output @reef urchin

[|, %]
torn lintel
#

My bad. It actually works in the split, but for some reason it doesn't work in contains():
System.out.println("|][%".contains("\\]\\["));
returns false

reef urchin
#

yes because split uses regex while contains uses normal strings

#

contains -> ][
split -> \\]\\[

#

\\]\\[ in regex means the same thing as ][ in a normal string

torn lintel
#

It works now. Thanks