vendredi 15 juillet 2016

Java Regex recursive match

I have a string like this:

IF(expr1,text1,IF(expr2,text2,IF(expr3,text3,text4)))

I need to process IF(expr3,text3,text4))) so the above become

 IF(expr1,text1,IF(expr2,text2,new_text))

I need to keep doing this until I have

 IF(expr1,text1,new_text3)

My pattern string "IF\(.*?,.*?,.*?\)" will match the whole thing. Not the IF(expr3,text3,text4))).

Java code:

String val = "IF(expr1,text1,IF(expr2,text2,IF(expr3,text3,text4)))";
String regx = "IF\(.*?,.*?,.*?\)";  
Pattern patt1 = Pattern.compile(regx);
Matcher m2 = patt1.matcher(val);
while (m2.find()) { 
    String val0 = m2.group();
    System.out.println( val0 ); 
}  

Aucun commentaire:

Enregistrer un commentaire