hsfrey has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to use a variable in a regex? Not the predefined variables, but my own variable. For instance, say I'd like to insert a sentence number at the start of each sentence. Of course I could do it in a loop with index and substr, but is there a way to do it with a global regex? ie:
my $sentenceCount=0; $str=s/pattern/..stuff..$sentenceCount++...stuff.../g;
Harvey

Replies are listed 'Best First'.
Re: Using variables in RegEx
by ikegami (Patriarch) on Aug 01, 2008 at 06:33 UTC

    First of all, there are no "predefined" variables. All variables can be interpolated. The replace expression in a string literal like any other.

    s/.../...$foo.../g;

    But you want to have Perl code in the replace expression. That's actually possible too. Using /e, the replace expression is treated as a Perl expression.

    s/.../$sentenceCount++; foo()/eg;

    $sentenceCount doesn't seem to make much sense with s///, though.

    $sentenceCount++ while /.../g;
      > First of all, there are no "predefined" variables. < The NAMES are predefined, eg $1, $', etc. > $sentenceCount doesn't seem to make much sense with s///, though. < I want to insert a unique html anchor for each sentence, so I need to increment the number to construct a unique name.

        The NAMES are predefined, eg $1, $', etc

        What I meant is that it's not a meaningful distinction. Since all variable should be declared, it's like saying "electronic television". Until they use laser circuitry for TVs, there's no such thing as electronic televisions. There are just televisions.

Re: Using variables in RegEx
by Anonymous Monk on Aug 01, 2008 at 06:58 UTC
      Actually, what might serve the OP's purpose is Named backreferences, a new feature of 5.10. For example, to primitively rearrange a date:
      $date = '8/1/2008'; $date =~ s!(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{4})!$+{year}-$ ++{month}-$+{day}!;
      This allows you to attach a name to a backreference like (?<name>pattern) that can be used within the match as \g{name} and anywhere else (in the replace or outside of the regex) as part of the hash %+. Keep in mind though, just because its a shiny new feature doesn't mean you have to use it. First figure out if its the best tool for the job :)

      Disclaimer: Of course, the example above is a quick and dirty example, and as such should never be used. It was only meant to illustrate the feature noted.

      Update: Ah, perhaps I did misunderstand the original question after all.

      __________
      Systems development is like banging your head against a wall...
      It's usually very painful, but if you're persistent, you'll get through it.

        I don't see how named references allow $sentenceCount++ to be executed in the replace expression.
Re: Using variables in RegEx
by dHarry (Abbot) on Aug 01, 2008 at 06:33 UTC

    I recall this was recently dicussed?! Have you already tried using the /e (e modifier) or even /ee (double e modifier)? You can also fiddle with the eval statement.