Is there a way to capture all instances of a regular expression match using the re library in Python in a recursive manner?

I am using the re library in Python 3.12 to capture multiple items from a F5 BIG-IP LTM configuration file to associate pools and members to wildcard certificates and store the data into a dictionary variable using .finditer() and .groupdict() since I want the key/value relationship to be maintained for later use in the script. I am having issues with one of the regular expressions not capturing all of the data that I need, but instead capturing only a single value.

Here is the regular expression that I am using:

ltmspools(?P<ltm_pool_name>/Common/.+)s{(?:s{n){0,}(?:n.+){1,2}memberss{(?:(?:n.+){0,1}/Common/(?P<ltm_pool_node_ipaddr>[0-9.]+):(?P<ltm_pool_node_port>[0-9]+)s{n.+n.+}){0,}

The sample data that this is being used against is stored in a file that is accessed by the with open(filenamehere.txt,"r"), but here is a block from that file that is giving me issues:

ltm pool /Common/pool_put-something.domain.here {
    members {
        /Common/10.20.30.1:443 {
            address 10.20.30.1
        }
        /Common/10.20.40.2:80 {
            address 10.20.40.2
        }
        /Common/10.20.50.3:8443 {
            address 10.20.50.3
        }
        /Common/10.20.60.4:8080 {
            address 10.20.60.4
        }
    }

When I run my Python script to grab the data using the regular expression mentioned earlier, it will capture only the last member:
/Common/10.20.60.4:8080

And not the rest of the members prior to it. If I modify the very last quantifier to be {0,1}, {0,2}, {0,3}, etc. it will then grab the associated member as the capture group but it will not grab each member otherwise.

When I use the following regular expression:
(?:n.+){0,1}/Common/(?P<ltm_pool_node_ipaddr>[0-9.]+):(?P<ltm_pool_node_port>[0-9]+)s{n.+n.+}

That will capture each of the members in that configuration block but then I am unable to grab the ltm pool /Common/pool_put-something.domain.here portion that I would like to relate this to. I need both of them to complete the last part of my script.

Below is the script itself:

import re
#
user_input = input("Enter the hostname of the F5 LTM device here:  ")
print("-----")
#
hostname = r"A:FilePathHere" + user_input + ".txt"
sys_cert_regex = re.compile(r"syssfilesssl-certs(?P<ssl_cert_name>.+)s{(?:n.+){0,12}expiration-strings"(?P<cert_exp>.+)"(?:n.+){0,10}serial-numbers(?P<cert_serial>.+)(?:n.+){0,3}subjects"(?P<cert_dn>.+)"(?:n.+){0,1}subject-alternative-names(?P<cert_san>.+)")
ltm_profile_clientssl_regex = re.compile(r"ltmsprofilesclient-ssls(?P<ltm_ssl_name>.+)s{(?:n.+){1,4}app-service(?:.+n.+){1,3}certs(?P<ltm_profile_cert>.+)n")
ltm_virtual_regex = re.compile(r"ltmsvirtuals(?P<ltm_virtual_name>.+)s{(?:n.+){1,2}destinations(?P<ltm_destination_name>.+)(?:n.+){1,9}pools(?P<ltm_pool_name>.+)n.+profiless{(?:n.+){0,7}/(?P<cert>.+)s{n.+contextsclientsiden")
ltm_pool_regex = re.compile(r"ltmspools(?P<ltm_pool_name>/Common/.+)s{(?:s{n){0,}(?:n.+){1,2}memberss{(?:(?:n.+){0,1}/Common/(?P<ltm_pool_node_ipaddr>[0-9.]+):(?P<ltm_pool_node_port>[0-9]+)s{n.+n.+}){0,}")
with open(hostname,"r") as file_obj:
    for sys_cert_match in sys_cert_regex.finditer(file_obj.read()):
        sys_cert = sys_cert_match.groupdict()
        if "*" in sys_cert["cert_dn"]:
            file_obj.seek(0)
            for ltm_profile_clientssl_match in ltm_profile_clientssl_regex.finditer(file_obj.read()):
                ltm_profile = ltm_profile_clientssl_match.groupdict()
                if sys_cert["ssl_cert_name"] in ltm_profile["ltm_profile_cert"]:
                    file_obj.seek(0)
                    for ltm_virtual_match in ltm_virtual_regex.finditer(file_obj.read()):
                        ltm_virtual = ltm_virtual_match.groupdict()
                        if ltm_virtual["cert"] in ltm_profile["ltm_ssl_name"]:
                            file_obj.seek(0)
                            for ltm_pool_match in ltm_pool_regex.finditer(file_obj.read()):
                                ltm_pool = ltm_pool_match.groupdict()
                                if ltm_pool["ltm_pool_name"] in ltm_virtual["ltm_pool_name"]:
                                    file_obj.seek(0)
                                    print("Certificate subject string:  ", sys_cert["cert_dn"])
                                    print("Certificate subject alternative names:  ", sys_cert["cert_san"])
                                    print("Certificate expiration:  ", sys_cert["cert_exp"])
                                    print("Name of the virtual pool using the certificate:  ", ltm_pool["ltm_pool_name"])
                                    print("IP address and associated port of a member in that pool:  ", ltm_pool["ltm_pool_node_ipaddr"], ":", ltm_pool["ltm_pool_node_port"])
                                    print("***")
            continue
        else:
            continue

I expect the print("IP address and associated port of a member in that pool: ", ltm_pool["ltm_pool_node_ipaddr"], ":", ltm_pool["ltm_pool_node_port"]) portion to print out every member, not just the first or last member, of every pool. Right now, it will only do the first or last member.

Is there a way to get both of these to work as a singular regular expression so that when I use the .finditer() and .groupdict() that it will store that into a single dictionary? If not, then is my next best way to achieve this is by putting each member into an array and then potentially taking that array and appending it as a value to a specific key?

New contributor

aa44414c is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật