pattern = re.compile(r"""
        (?P<any>any4?)                                       # "any"
                                                             #   association
        |                                                    # or
        (?P<object_eq>object ([\w-]+) eq (\d+))             # object
        alone
                                                             #   association
        |                                                    # or
        (?P<object_range>object ([a-z0-9A-Z-]+) range (\d+) (\d+)) # object range
                                                             #   association
        |                                                    # or
        (?P<object_group>object-group ([a-z0-9A-Z-]+))             # object group
                                                             #   association
        |                                                    # or
        (?P<object_alone>object ([[a-z0-9A-Z-]+))                   # object alone
                                                             #   association
""", re.VERBOSE)

s = '''    object-group jfi-ip-ranges object DA-TD-WEB01 eq 8850
'''

"""
Explanation: Let's start with the original regular expression and
string to search from Travis'
regex problem.
End of explanation
"""


pattern = re.compile(r"""
        (?P<any>any4?)                                       # "any"
                                                             #   association
        |                                                    # or
        (?P<object_eq>object\ ([\w-]+)\ eq\ (\d+))             # object
        alone
                                                             #   association
        |                                                    # or
        (?P<object_range>object\ ([a-z0-9A-Z-]+)\ range\ (\d+)\ (\d+)) # object range
                                                             #   association
        |                                                    # or
        (?P<object_group>object-group\ ([a-z0-9A-Z-]+))             # object group
                                                             #   association
        |                                                    # or
        (?P<object_alone>object\ ([a-z0-9A-Z-]+))                   # object alone
                                                             #   association
""", re.VERBOSE)

re.findall(pattern, s)

for m in re.finditer(pattern, s):
    print(repr(m))
    print('groups', m.groups())
    print('groupdict', m.groupdict())

"""
Explanation: The regex had two bugs.
- Two [[ near the end of the pattern string.
- The significant spaces in the pattern (such as after object-group) were being ignored because of re.VERBOSE.
So those bugs are fixed in the pattern below.
End of explanation
"""


pattern = re.compile(r"""
        (?P<any>any4?)                                       # "any"
                                                             #   association
        |                                                    # or
        (?P<object_eq>object\ (?P<oe_name>[\w-]+)\ eq\ (?P<oe_i>\d+))             # object
        alone
                                                             #   association
        |                                                    # or
        (?P<object_range>object\ (?P<or_name>[a-z0-9A-Z-]+)
        \ range\ (?P<oe_r_start>\d+)\ (?P<oe_r_end>\d+)) # object range
                                                             #   association
        |                                                    # or
        (?P<object_group>object-group\ (?P<og_name>[a-z0-9A-Z-]+))             # object group
                                                             #   association
        |                                                    # or
        (?P<object_alone>object\ (?P<oa_name>[a-z0-9A-Z-]+))                   # object alone
                                                             #   association
""", re.VERBOSE)

for m in re.finditer(pattern, s):
    print(repr(m))
    print('groups', m.groups())
    print('groupdict', m.groupdict())

"""
Explanation: The above works, but keeping track of the indexes of the unnamed groups drives me crazy. So I add names for all groups.
End of explanation
"""


for m in re.finditer(pattern, s):
    for key, value in m.groupdict().items():
        if value is not None:
            print(key, repr(value))
    print()

"""
Explanation: The following shows me just the groups that matched.
End of explanation
"""


pattern = re.compile(r"""
        (?P<any>any4?)                                       # "any"
                                                             #   association
        |                                                    # or
        (object\ (?P<oe_name>[\w-]+)\ eq\ (?P<oe_i>\d+))             # object
        alone
                                                             #   association
        |                                                    # or
        (object\ (?P<or_name>[a-z0-9A-Z-]+)
        \ range\ (?P<oe_r_start>\d+)\ (?P<oe_r_end>\d+)) # object range
                                                             #   association
        |                                                    # or
        (object-group\ (?P<og_name>[a-z0-9A-Z-]+))             # object group
                                                             #   association
        |                                                    # or
        (object\ (?P<oa_name>[a-z0-9A-Z-]+))                   # object alone
                                                             #   association
""", re.VERBOSE)

"""
Explanation: Looking at the above,
I see that I probably don't care about the big groups,
just the parameters,
so I remove the big groups (except for "any")
from the regular expression.
End of explanation
"""


for m in re.finditer(pattern, s):
    for key, value in m.groupdict().items():
        if value is not None:
            print(key, repr(value))
    print()

"""
Explanation: Now it tells me just the meat of what I want to know.
End of explanation
"""


