Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression for finding string between [[ ]] and {{}}

Tags:

java

regex

How could I find string between [[ ]]? After searching a answer I find this method below, but I cannot find the regular expression for [[ ]], neither for {{ }}.

What I want to do is to find e.g. [[abc]]XXXXXXXXXX[[def]], and save abc, def as a ArrayList.

String s = "[[abc]]XXXXXXXXXX[[def]]";
Pattern p = Pattern.compile("[[(.*?)]]")
Matcher m = p.matcher(s);
if (m.find()) {
    System.out.println(m.group(i)); // => "abc","def"
}
like image 897
user2694404 Avatar asked Mar 27 '26 21:03

user2694404


1 Answers

You must double escape the opening square brackets (you can do the same with the closing) since they are used to define character classes:

Pattern p = Pattern.compile("\\[\\[(.*?)]]");

(exactly the same with curly braces, that are used for quantifiers)

You can read this fabulous post about the incredible world of square brackets.

like image 77
Casimir et Hippolyte Avatar answered Mar 29 '26 12:03

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!