简体   繁体   中英

Extracting text inside “[[ ]]” in java

I have a string which contains text inside parenthesis. I need to extract text present inside "[[ ]]" parenthesis using Java. Also, there are multiple occurrences of "[[ ]]" parenthesis. I would like to extract text from all of them.

For example:

    String text = "[[test]] {{test1}} [[test2]]"; 

Expected Output: test test2

Can anyone help please?

It's a simple regular expression match:

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

Use a Matcher with lookingAt() method to get the result.

To remove the "[[" and "]]" after that, just add a String#replace() .

you can use this:

String text = "[[test]] {{test1}} [[test2]]";
Pattern p = Pattern.compile("\\[\\[(.*?)]]", Pattern.DOTALL);
Matcher m = p.matcher(text); 
while (m.find()) {
    System.out.print(m.group(1));
}

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.

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