fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. class Test
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. Character markStart = '{';
  9. Character markEnd = '}';
  10. Boolean includeMarkers = true;
  11. int offset = (includeMarkers ? -1 : 0);
  12. String s = "Some Garbage text\r\nltm virtual The_Name_51244_sdfasfdasd {\r\n address-status yes\r\n enabled\r\n fallback-persistence none\r\n profiles {\r\n /Common/GLOBAL_PROFILE {\r\n context all\r\n }\r\n /Common/http {\r\n context all\r\n }\r\n }\r\n rate-class none\r\n rules {\r\n /Common/X-F-F\r\n }\r\n log-profiles none\r\n source-address-translation {\r\n pool SOME-SNAT-POOL\r\n type snat\r\n }\r\n source-port preserve\r\n vlans {\r\n Vlan1111\r\n }\r\n service-down-immediate-action none\r\n service-policy none\r\n source 0.0.0.0/0\r\n}\r\nbarbage text\r\nltm virtual The_Object_51244 {\r\n address-status yes\r\n enabled\r\n fallback-persistence none\r\n profiles {\r\n /Common/GLOBAL_PROFILE {\r\n context all\r\n }\r\n /Common/http {\r\n context all\r\n }\r\n }\r\n rate-class none\r\n rules {\r\n /Common/X-F-F\r\n }\r\n log-profiles none\r\n source-address-translation {\r\n pool SOME-SNAT-POOL\r\n type snat\r\n }\r\n source-port preserve\r\n vlans {\r\n Vlan2222\r\n }\r\n service-down-immediate-action none\r\n service-policy none\r\n source 0.0.0.0/0\r\n}\r\nTrailing garbage text";
  13. Pattern patternBefore = Pattern.compile("(ltm\\s+virtual)\\s+([\\w-]+)\\s+" + Pattern.quote(markStart.toString()));
  14.  
  15. Matcher m = patternBefore.matcher(s);
  16. while (m.find()) {
  17. System.out.println("Group 1: " + m.group(1));
  18. System.out.println("Group 2: " + m.group(2));
  19. String res = getBalancedSubstring(s.substring(m.end()), markStart, markEnd, includeMarkers);
  20.  
  21. System.out.println("Found nested: " + res + "\n----");
  22. if (res == null) {
  23. System.out.println("No nested parens match found, this match must be failed.");
  24. }
  25. else {
  26. s = s.substring(m.end()+res.length() + offset);
  27. m.reset(s);
  28. }
  29. }
  30. }
  31. public static String getBalancedSubstring(String s, Character markStart, Character markEnd, Boolean includeMarkers)
  32. {
  33. int level = 1;
  34. for (int i = 0; i < s.length(); i++) {
  35. char c = s.charAt(i);
  36. if (c == markStart) {
  37. level++;
  38. }
  39. else if (c == markEnd) {
  40. if (level == 1) {
  41. return (includeMarkers ? markStart.toString() : "") + s.substring(0, (includeMarkers ? i + 1 : i));
  42. }
  43. if (level > 0) level--;
  44. }
  45. }
  46. return null;
  47. }
  48. }
Success #stdin #stdout 0.16s 50792KB
stdin
Standard input is empty
stdout
Group 1: ltm virtual
Group 2: The_Name_51244_sdfasfdasd
Found nested: {
    address-status yes
    enabled
    fallback-persistence none
    profiles {
        /Common/GLOBAL_PROFILE {
            context all
        }
        /Common/http {
            context all
        }
    }
    rate-class none
    rules {
        /Common/X-F-F
    }
    log-profiles none
    source-address-translation {
        pool SOME-SNAT-POOL
        type snat
    }
    source-port preserve
    vlans {
        Vlan1111
    }
    service-down-immediate-action none
    service-policy none
    source 0.0.0.0/0
}
----
Group 1: ltm virtual
Group 2: The_Object_51244
Found nested: {
    address-status yes
    enabled
    fallback-persistence none
    profiles {
        /Common/GLOBAL_PROFILE {
            context all
        }
        /Common/http {
            context all
        }
    }
    rate-class none
    rules {
        /Common/X-F-F
    }
    log-profiles none
    source-address-translation {
        pool SOME-SNAT-POOL
        type snat
    }
    source-port preserve
    vlans {
        Vlan2222
    }
    service-down-immediate-action none
    service-policy none
    source 0.0.0.0/0
}
----