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


In reply to Re: bracket processing by kcott
in thread bracket processing by rajaman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.