Question:
How to print the text inside the brackets from the string: "Here is [some] text"
Answer:
Using a Matcher
Code:
import java.util.regex.*; public class Delimiter { public static void main(String[] args) { String str = "Here is [some] text"; System.out.println("str: " + str); Matcher m = Pattern.compile("\\[([^)]+)\\]").matcher(str); while(m.find()) { System.out.println(m.group(1)); } } }
Output:
$ java Delimiter str: Here is [some] text some