in reply to bracket processing
G'day rajaman,
"I also tried to work with 'Text::Balanced' module but couldn't make it work."
If you were more specific about what "make it work" means, and also posted the code you tried, we could probably be more helpful. Here's a couple of examples of what you could have done.
#!/usr/bin/env perl use strict; use warnings; use Text::Balanced 'extract_bracketed'; my $delim = '([{<'; my $prefix = qr{[^$delim]*}; my $string = 'The use of parentheses (indicates that the (writer [cons +idered] the {information}) less <important—almost> an afterthought).' +; my @parts = extract_bracketed($string, $delim, $prefix); print " i) $parts[2]$parts[1]\n"; print "ii) $parts[0]\n"; my ($trimmed_start) = $parts[2] =~ /^(.*?)\s*$/; print " I) $trimmed_start$parts[1]\n"; print "II) $parts[0]\n";
Output:
i) The use of parentheses . ii) (indicates that the (writer [considered] the {information}) less < +important—almost> an afterthought) I) The use of parentheses. II) (indicates that the (writer [considered] the {information}) less < +important—almost> an afterthought)
The first two results (i & ii) are what I believe you asked for but not what your expected output showed: note the extra space at the end (parentheses .).
The second two results (I & II) show how you might trim that extra space. This looks more like the expected output you show but doesn't exactly follow your description.
It would be helpful if you presented your expected output within <code>...</code> tags, so that we can see more clearly exactly what you want (HTML paragraph rendering is not always faithful to the original text).
It would also help if you supplied a range of much shorter input samples, along with your expected output for these. Consider edge cases: no text before the first bracket; no text after the last bracket; unbalanced brackets in various places; and so on.
See also: Text::Balanced.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: bracket processing
by AnomalousMonk (Archbishop) on Mar 31, 2020 at 06:09 UTC | |
by kcott (Archbishop) on Apr 01, 2020 at 00:37 UTC | |
by AnomalousMonk (Archbishop) on Apr 01, 2020 at 18:23 UTC | |
by kcott (Archbishop) on Apr 02, 2020 at 01:44 UTC | |
by AnomalousMonk (Archbishop) on Apr 02, 2020 at 03:16 UTC |