Regular expression to obtain value from [[text]]. I have tried the regex
"((?<=[[)*(?=]])*)+" the value between [[ ]] is not obtained.
For example, from the string [[text]], we should obtain text.
Pattern pat = Pattern.compile("((?<=\\[[)*(?=\\]])*)");
Matcher matcher = pat.matcher("[[text]]");
String next ="";
while(matcher.find()) {
next = matcher.group(0);
break;
}
System.out.println(next); //next should be text
You need to escape brackets []
when using them as actual characters in a regular expression. And you also need to add something to actually capture what is between the brackets. You can use .*
for that or use my approach, if you are sure the value cannot contain a ]
.
((?<=\[\[)([^\]]*)(?=\]\]))+
There is not even really a need to use lookbacks und lookaheads unless you explictly need to exempt those limiters from the match. This will work just as well:
\[\[([\]]*\]\]
And obviously when you put these into a String, you need to add additional \\
to escape the \\
for the String...they are just more readable this way.
如果您不想使用regex
, String.replaceAll
也可以为您提供帮助。
String s2 = s.replaceAll("\\[", "").replaceAll("\\]", "");
"(?<=\\[\\[)[^\\]]*"
这应该为你工作
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.