正则表达式匹配大括号(Java)

public static void main(String[] args) {
		String regex = "\\{([^}])*\\}";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher("ab{gnfnm}ah{hell}o");
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}

输出: 

{gnfnm}
{hell}

\\{ ( [  ^}  ] * )   \\ }

  • \\{表示匹配{,{是元字符,要用\转义
  • ()用来指定子表达式。如果是单个字符比如匹配0个或多个A,要用A*,如果这里A也是一个表达式的话要用()括起来,后面再加限定符
  • []中可以直接列出要匹配的字符或数字,[aeiou]匹配元音字母,[0-9]匹配0到9,作用同\d
  • ^}表示匹配 } 以外的字符

你可能感兴趣的:(Java)