import java.util.*;
import java.util.regex.*;

class Test
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Character markStart = '{';
		Character markEnd = '}';
		Boolean includeMarkers = true;
		int offset = (includeMarkers ? -1 : 0);
		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";
		Pattern patternBefore = Pattern.compile("(ltm\\s+virtual)\\s+([\\w-]+)\\s+" + Pattern.quote(markStart.toString()));
		
		Matcher m = patternBefore.matcher(s);
		while (m.find()) {
			System.out.println("Group 1: " + m.group(1));
			System.out.println("Group 2: " + m.group(2));
			String res = getBalancedSubstring(s.substring(m.end()), markStart, markEnd, includeMarkers);
			
			System.out.println("Found nested: " + res + "\n----");
			if (res == null) {
				System.out.println("No nested parens match found, this match must be failed.");
			}
			else {
				s = s.substring(m.end()+res.length() + offset);
				m.reset(s);
			}
		}
	}
	public static String getBalancedSubstring(String s, Character markStart, Character markEnd, Boolean includeMarkers) 
	{
	    int level = 1;
	    for (int i = 0; i < s.length(); i++) {
	        char c = s.charAt(i);
	        if (c == markStart) {
	            level++;
	        }
	        else if (c == markEnd) {
	            if (level == 1) {
	                return (includeMarkers ? markStart.toString() : "") + s.substring(0, (includeMarkers ? i + 1 : i));
	            }
	            if (level > 0) level--;
	        }
	    }
	    return null;
    }
}