S
somedeveloper
Hi,
Would appreciate some hints on a 'smart' / 'nifty' solution to this
problem.
The problem:
I need to extract a block of text lying between -- let's say -- a
pair of brackets.
There can be an arbitrary # of such [] blocks nested one inside the
other.
I know how to mark my first '[' to start the matching process.
Example:
abc [ def .*
[ .* ]
[ .*
[ .* ]
]
uvw ] xyz
Desired output: [ def .* uvw ]
1. Now, I don't know if this is something Perl regexps can handle. I
read somewhere (possibly incorrectly) that nested patterns are in
general constructs that are handled via grammars (flex/bison combo)
and not regexps.
2. But since Perl provides features like match-time-code-evaluation in
regexps, I thought incrementing a count variable on each '[',
decrementing it on each ']', and printing the current pattern when the
count goes to zero would do the job... but I'm not so sure how.
3. If there's really no solution via regexps and grammars, I would
have to use the brute-force approach of processing each character in a
loop looking for ['s and ]'s. (yuck!)
Regards...
Would appreciate some hints on a 'smart' / 'nifty' solution to this
problem.
The problem:
I need to extract a block of text lying between -- let's say -- a
pair of brackets.
There can be an arbitrary # of such [] blocks nested one inside the
other.
I know how to mark my first '[' to start the matching process.
Example:
abc [ def .*
[ .* ]
[ .*
[ .* ]
]
uvw ] xyz
Desired output: [ def .* uvw ]
1. Now, I don't know if this is something Perl regexps can handle. I
read somewhere (possibly incorrectly) that nested patterns are in
general constructs that are handled via grammars (flex/bison combo)
and not regexps.
2. But since Perl provides features like match-time-code-evaluation in
regexps, I thought incrementing a count variable on each '[',
decrementing it on each ']', and printing the current pattern when the
count goes to zero would do the job... but I'm not so sure how.
3. If there's really no solution via regexps and grammars, I would
have to use the brute-force approach of processing each character in a
loop looking for ['s and ]'s. (yuck!)
Regards...