0

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
2
  • please develop your answer... Explain better your problem, show code, etc... Commented Mar 4, 2013 at 15:54
  • What @MatiasCaamaño said -- suggestion: give an example. And can the value contain single ] characters? Commented Mar 4, 2013 at 15:56

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't wanna get into regex, String.replaceAll can also help you.

String s2 = s.replaceAll("\\[", "").replaceAll("\\]", "");

Comments

1
"(?<=\\[\\[)[^\\]]*"

this should work for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.