in reply to Matching a pattern in Regex

This is a good use for the /e option on the substitution operator.

use strict; use warnings; $_ = <DATA>; if (/<figr n="/) { s[<figr n="(\d+)">(.*?)</figr>] [qq(<FIGIND NUM="$1" ID="FG.) . sprintf('%03d', $1) . qq(">$2</FIGI +ND>)]eg; } print; __DATA__ 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">F +igure 1</figr>. Like all other cells in the body, it has a nucleus th +at contains genetic information.<figr n="2">Figure 2</figr>. The cell + is covered by a membrane and is filled with a fluid.
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Matching a pattern in Regex
by Ieronim (Friar) on Jul 25, 2006 at 10:40 UTC
    There is IMHO no sense in the 'if' condition in your snippet :) Or do i miss something?

         s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print

      Well, it was there (albeit in a more complex form) in the original code, so I thought I'd simplify it but leave it there.

      If there's a lot of text and substitutions are only carried out on a small number of lines, then it's probably worth doing a simple check like this - but I'd almost certainly leave it out if I was writing this code from scratch.

      As in all cases, the only sure way to know if it's useful is to benchmark the options with something similar to the real data.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg