有这么一个字符串:a1=1,a2=X2{b1=1,b2=X3{c1=1,c2=2}},a3=X4{b1=1}
想写一个正则表达式,让下面code的执行结果是:
result(以逗号为token来分割字符串,逗号在"{"和"}"里面的忽略其做为token):
[code]
a1=1
a2=X2{b1=1,b2=X3{c1=1,c2=2}}
a3=X4{b1=1}
[/code]
code:
[code]
Pattern p1 = Pattern.compile("(?<=^|,)(?:[^,{]++(?:\\{[^{}]*+(?:\\{[^{}]++\\})*+\\})*+)*+(?=,|$)");
Matcher m1 = p1.matcher("a1=1,a2=X2{b1=1,b2=X3{c1=1,c2=2}},a3=X4{b1=1}");
while( m1.find() )
{
String foundstring = m1.group();
System.out.println("foundstring:" + foundstring);
}
[/code]
用正则表达式实现上面需求确实比较无奈,还是要动用到ANTLR定义自己的语言规则比较好?
想写一个正则表达式,让下面code的执行结果是:
result(以逗号为token来分割字符串,逗号在"{"和"}"里面的忽略其做为token):
[code]
a1=1
a2=X2{b1=1,b2=X3{c1=1,c2=2}}
a3=X4{b1=1}
[/code]
code:
[code]
Pattern p1 = Pattern.compile("(?<=^|,)(?:[^,{]++(?:\\{[^{}]*+(?:\\{[^{}]++\\})*+\\})*+)*+(?=,|$)");
Matcher m1 = p1.matcher("a1=1,a2=X2{b1=1,b2=X3{c1=1,c2=2}},a3=X4{b1=1}");
while( m1.find() )
{
String foundstring = m1.group();
System.out.println("foundstring:" + foundstring);
}
[/code]
用正则表达式实现上面需求确实比较无奈,还是要动用到ANTLR定义自己的语言规则比较好?