Generally using regexen for parsing markup is tricky and best left to modules such as HTML::TreeParser. If you can absolutely predict what the markup will be then you may get away with the following:

use warnings; use strict; my $str = <<TXT; Nerve cells come in many shapes and sizes, but they all have a number +of identifiable parts. A typical nerve cell is shown in <figr n="1">Figur +e 1</figr>. Like all other cells in the body, it has a nucleus that cont +ains genetic information.<figr n="2">Figure 2</figr>. The cell is covered b +y a membrane and is filled with a fluid. TXT $str =~ s/ <figr\sn="(\d+?)"> # tag including figure number (.*?) # element contents <\/figr> # close tag / "<FIGIND NUM=\"$1\" ID=\"FG." . # replacement tag sprintf("%03d", $1) . # padded number "\">$2<\/FIGIND>" # remainder of element /gmsxe; # Global, multi-line, ignore newline, ignore whitespac +e, evaluate print $str;

Prints:

Nerve cells come in many shapes and sizes, but they all have a number +of identifiable parts. A typical nerve cell is shown in <FIGIND NUM="1" I +D="FG.001">Figure 1</FIGIND>. Like all other cells in the body, it has a nucleus that co +ntains genetic information.<FIGIND NUM="2" ID="FG.002">Figure 2</FIGIND>. The + cell is covered by a membrane and is filled with a fluid.

DWIM is Perl's answer to Gödel

In reply to Re: Matching a pattern in Regex by GrandFather
in thread Matching a pattern in Regex by rsriram

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.