Hi, In my markup file, I want to check for a occurrence which matches, <figr n="(.+?)">(.*?)<\/figr> and replace it with the below mentioned tag.

if($_ =~ /<figr n="(.+?)">(.*?)<\/figr>/)
    {
        $fno=$1;
        $figno=sprintf("%03d", $fno);
        $_ =~ s/<figr n="(.+?)">(.*?)<\/figr>/<FIGIND NUM="$fno" ID="FG.$figno">$2<\/FIGIND>/g;
    }

The original value of figr in the markup file should be filled with preceeding zero's to make it a 3 digit number. My problem is, if I have two <figr n="#"> elements in a same line, the value of the first indicator is getting used for the second indicator too.

For example,

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">Figure 1</figr>. Like all other cells in the body, it has a nucleus that contains genetic information.<figr n="2">Figure 2</figr>. The cell is covered by a membrane and is filled with a fluid.

The output is created as:

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" ID="FG.001">Figure 1</FIGIND>. Like all other cells in the body, it has a nucleus that contains genetic information.<FIGIND NUM="1" ID="FG.001">Figure 2</FIGIND>. The cell is covered by a membrane and is filled with a fluid.

Can anyone tell me what's wrong in my code? I also tried using <figr n="([^"]+)"> in search instead of the above pattern.


In reply to 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.