I have MS Word documents that I have passed all the contents to an SQL table.
Content contains multiple square brackets and curly braces, e.g.
[{a} as at [b],] {c,} {d,} etc
and I need to do a check to make sure that the brackets are balanced / matched, for example. below the content should return false:
- [{a} as at [b], {c,} {d,}
- ][{a} as at [b], {c,} {d,}
- [{a} as at [b],] {c,} }{d,
What I have done so far, extracts all the brackets and stores their information in the SQL table, as shown below:
(paragraph number, type of bracket, position of the bracket, level of the bracket)
3 [ 8 1
3 ] 18 0
3 [ 23 1
3 ] 35 0
7 [ 97 1
7 ] 109 0
7 [ 128 1
7 { 129 2
7 } 165 1
7 [ 173 2
7 ] 187 1
7 ] 189 0
7 { 192 1
7 } 214 0
7 { 216 1
7 } 255 0
7 { 257 1
7 } 285 0
7 { 291 1
7 } 326 0
7 { 489 1
7 } 654 0
I am not sure how the algorithm will work to check if the brackets are balanced in each paragraph, and give an error message if it is not.
Any advice would be appreciated!
EDIT:
The code should work for the following scenario:
( , , , )
15 [ 543 1
15 { 544 2
15 } 556 1
15 [ 560 2
15 ] 580 1
15 ] 581 0
15 [ 610 1
15 ] 624 0
15 [ 817 1
15 ] 829 0