Home > Back-end >  Find all strings between top level brackets {}
Find all strings between top level brackets {}

Time:12-23

I have some string values formatted like this:

# ltm virtuals above
ltm virtual Common/vserver.enterprise.com {
    destination Common/vserver.enterprise.com
    ip-protocol tcp
    mask x.x.x.x
    partition Common
    persist {
        Common/cookie_30_minutes {
            default yes
        }
    }
    pool Common/prod.enterprise.com
    profiles {
        Common/prod.enterprise.com-clientssl {
            context clientside
        }
        Common/prod.enterprise.com_https { }
        Common/oneconnect { }
        Common/tcp-lan-optimized { }
    }
    rules {
        Common/restrict_public_addresses
        Common/pool_members_list
    }
    source 0.0.0.0/0
    source-address-translation {
        type automap
    }
    translate-address enabled
    translate-port enabled
    vs-index 57
}
# other values just like the above with different rules, virtual servers, etc, repeat

I need to figure out how to get each one of these blocks, but only if they contain

rules {
    Common/restrict_on_site_access_building_2
    # could be more rules but I wouldn't care
}

So if that rule is found, return the entire block from "ltm virtual" to the last closing bracket. I've tried several regex's but I keep getting tripped up by the either opening and closing brackets in the other portions of "ltm virtual" or I jump over the entire block and into another set of ltm virtual block(s).

CodePudding user response:

If I understand you correctly, and these instructions come to you as a string and you want to extract the piece if string that you have written. then this regex should work

(rules \{((.|\n)*?)Common\/restrict_public_addresses((.|\n)*?)\})

you can try it on https://regex101.com/

CodePudding user response:

I would split the text by using "ltm virtual " as the delimiter between blocks. Then just search for the text to find by using the in keyword. In this case I have just printed the result. However, it would be just as easy to append to a list or equivalent structure (if required).

Code:

import re

text_to_find = "Common/restrict_on_site_access_building_2"

ltm_blocks = re.split('ltm virtual ', text)
for lines in ltm_blocks:
    if text_to_find in lines:
        result = "ltm virtual "  lines
        print(result)

Output:

ltm virtual Common/vserver.enterprise.com {
    destination Common/vserver.enterprise.com
    ip-protocol tcp
    mask x.x.x.x
    partition Common
    persist {
        Common/cookie_30_minutes {
            default yes
        }
    }
    pool Common/prod.enterprise.com
    profiles {
        Common/prod.enterprise.com-clientssl {
            context clientside
        }
        Common/prod.enterprise.com_https { }
        Common/oneconnect { }
        Common/tcp-lan-optimized { }
    }
    rules {
        Common/restrict_public_addresses
        Common/restrict_on_site_access_building_2
    }
    source 0.0.0.0/0
    source-address-translation {
        type automap
    }
    translate-address enabled
    translate-port enabled
    vs-index 57
}
  • Related