用正则表达式匹配括号中的内容,不包含括号,例如:(hello) ,只要 hello 不要括号,正则表达式如何写呢?
 
复制代码 代码示例:
public static void main(String[] args) {
 String content = "(hello)";
 String regex = "(?<=().*(?=))";
 Pattern p = Pattern.compile(regex);
 Matcher m = p.matcher(content);
 while (m.find()) {
     System.out.println(m.group());
 }
    }
 
public static void main(String[] args) {
 String content = "(hello)";
 String regex = "[^()]+";
 Pattern p = Pattern.compile(regex);
 Matcher m = p.matcher(content);
 while (m.find()) {
     System.out.println(m.group());
 }
    }
 
追问
我使用的程序只能用  [^()]+ ,但是如果括号前面有内容,象 good(hello),会先匹配到 good ,如何改写一下直接匹配到 hello ?
 
复制代码 代码示例:
 public static void main(String[] args) {
  String content = "good(hello)";
  String regex = "((.*?))";
  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(content);
  while (m.find()) {
   System.out.println(m.group(1));
  }
 }
 
也许正则可以用,只是对括号的转移不一样。