简体   繁体   English

用于在[[]]和{{}}之间查找字符串的正则表达式

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

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 eg [[abc]]XXXXXXXXXX[[def]] , and save abc , def as a ArrayList. 我想要做的是找到例如[[abc]]XXXXXXXXXX[[def]] ,并将abcdef保存为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"
}

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. 你可以阅读这篇关于令人难以置信的方括号世界的精彩帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM