in reply to Using variables in RegEx

perlretut - Perl regular expressions tutorial

Replies are listed 'Best First'.
Re^2: Using variables in RegEx
by EvanK (Chaplain) on Aug 01, 2008 at 21:47 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.