fork download
  1. import java.util.*;
  2.  
  3. import java.util.regex.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String regex = "(\\d+)\\.(\\d+)(?:\\.(\\d+))?";
  10. Pattern p = Pattern.compile(regex);
  11.  
  12. String s1="3.2.1";
  13. Matcher m1 = p.matcher(s1);
  14. String result1 = m1.replaceAll(x ->
  15. x.group(3) != null ? "OGNL_" + x.group(1) + "_" + x.group(2) + "_" + x.group(3) :
  16. "OGNL_" + x.group(1) + "_" + x.group(2) );
  17. System.out.println(result1);
  18.  
  19. String s2="3.2";
  20. Matcher m2 = p.matcher(s2);
  21. String result2 = m2.replaceAll(x ->
  22. x.group(3) != null ? "OGNL_" + x.group(1) + "_" + x.group(2) + "_" + x.group(3) :
  23. "OGNL_" + x.group(1) + "_" + x.group(2) );
  24. System.out.println(result2);
  25. }
  26. }
Success #stdin #stdout 0.23s 38220KB
stdin
Standard input is empty
stdout
OGNL_3_2_1
OGNL_3_2