import re
def parse_array_indexes(path):
# Regular expression to match array indexes or keys
pattern = re.compile(r'\[(.*)\]')
# Find all matches in the path
matches = pattern.findall(path)
return matches
# Example usage
paths = [
'foo.bar.baz[2].qux', # Positive example
'foo.bar[myKey].baz', # Positive example
'foo.bar.baz.qux', # Negative example
'foo.bar.baz[2.qux', # Negative example (missing closing bracket)
'foo.bar.baz2].qux', # Negative example (missing opening bracket)
'foo.bar.baz[2]qux', # Positive example
'foo.bar.baz[2].qux[myKey]',# Positive example
'foo.bar]myKey[.baz', # Negative example (brackets out of order)
'foo.bar.baz[2].qux]', # Negative example (extra closing bracket)
'[foo].bar.baz[2].qux', # Negative example (brackets around non-index)
'foo.bar.baz[2][myKey].qux',# Positive example
'foo.bar[o[3]].baz'
]
for path in paths:
print(f"{path}: {parse_array_indexes(path)}")
Click Run or press shift + ENTER to run code