I'm having a little trouble understanding your goals here, so correct me if the following code does not address your issue. I am reading this as you wish to locate all occurrences of [Sidenote: *] and extract the unique text, displaying it surrounded by <div> tags. A (re)read of perlretut might be useful for you, but the following code does what I describe above.

use strict; use warnings; my $text = "C:\\letters.txt"; my @letters; open IN, '<', $text or die "Can't open $text"; @letters = <IN>; close(IN); chomp @letters; foreach my $indiv_note (@letters) { if ($indiv_note =~ /\[Sidenote\:\s(.*?)\]/) { print "<div>$1</div>\n"; } }

Note that the way I have written this, sidenotes cannot cross line boundaries, though doing this would be fairly trivial. Also note that if you want to include [ and ] in your posts, you should use html entities, i.e. &#91; and &#93;


In reply to Re: Regexing a block of text in between patterns by kennethk
in thread Regexing a block of text in between patterns by Anonymous Monk

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.