Pyparsing improved after a discussion of comp.lang.python, and I think that even Cameron Laird will not complain about the solution using the pyparsing nestedExpr method:
OUTPUT = """{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}} {{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic "Item 1"}}}""" from pyparsing import nestedExpr, originalTextFor nestedBraces1 = nestedExpr('{', '}') for nb in nestedBraces1.searchString(OUTPUT): print nb nestedBraces2 = originalTextFor(nestedExpr('{', '}')) for nb in nestedBraces2.searchString(OUTPUT): print nb
Print
[[['172.25.50.10:01:01-Ethernet', '172.25.50.10:01:02-Ethernet', ['Traffic', 'Item', '1']]]] [[['172.25.50.10:01:02-Ethernet', '172.25.50.10:01:01-Ethernet', ['Traffic', '"Item 1"']]]] ['{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}}'] ['{{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic "Item 1"}}}']
If you need to re-print the data to get the individual elements from the nested curly brackets, then the initial output of the nested list from nestedExpr may be better (note that even if the list contains quotation marks, the specified element is saved as its own element). But if you really want this line containing nested curly braces, use the originalTextFor form shown in nestedBraces2 .
source share