in reply to Re: bracket processing
in thread bracket processing
You could just use a loop, dealing with each bracketed part that is encountered. Here's a basic technique:
#!/usr/bin/env perl use strict; use warnings; use constant { EXTRACTED => 0, SUFFIX => 1, PREFIX => 2, }; use Text::Balanced 'extract_bracketed'; my $delim = '([{<'; my $prefix = qr{[^$delim]*}; my $string = 'The (use {of}) parentheses (indicates that the (writer [ +considered] the {information}) less <important—almost> an afterthough +t).'; my ($current, $wanted) = ($string, ''); while (1) { my @parts = extract_bracketed($current, $delim, $prefix); if (defined $parts[PREFIX]) { my ($trimmed_start) = $parts[PREFIX] =~ /^(.*?)\s*$/; $wanted .= $trimmed_start; } if (defined $parts[EXTRACTED]) { $current = $parts[SUFFIX]; } else { $wanted .= $parts[SUFFIX]; last; } } print "$wanted\n";
Output:
The parentheses.
I'll leave you to integrate that technique into your actual production code.
[The lack of sample data — as requested by myself and expanded upon by AnomalousMonk — was disappointing.]
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: bracket processing
by rajaman (Sexton) on Apr 02, 2020 at 02:16 UTC | |
by AnomalousMonk (Archbishop) on Apr 02, 2020 at 04:09 UTC | |
by rajaman (Sexton) on Apr 05, 2020 at 21:00 UTC |