in reply to capture global substitution in an array

What exactly do you mean by "capture the substitutions"?

A substitution is an action, not something you can express in a perl scalar. Only its return value and the result of the string it acted upon are easily captured.

  • Comment on Re: capture global substituion inot an array

Replies are listed 'Best First'.
Re^2: capture global substituion inot an array
by wrinkles (Pilgrim) on Apr 29, 2012 at 17:39 UTC

    Yes, I suspected that the real problem lies in my misunderstanding. I imagined that there is a mythical "replace" string that substitutes for the matched string. The "replace" of a regex replace is the entire resulting string, and not some discrete "diff" element. Thanks for helping me understand that better.

    Perhaps what I am really looking to do (although it may be ill-conceived) is:

    • Capture the array of matched substrings based on the first modifier argument
    • loop through the array, performing a regex substitution on each substring based on the second argument.
    • return the list of modified substrings

    This algorithm is certainly not a standard "regex substitution". I'll have to think more about this to determine if it even makes sense to do. But at least you have helped me define the problem. Thanks!

      So now I'm thinking that the tag modifier needs three arguments:
      • The match regex expression to extract the substrings.
      • The substitution regex to perform on these captured substrings.
      • The replace expression.
      Is that as crazy as it sounds to me? :)
Re^2: capture global substituion inot an array
by wrinkles (Pilgrim) on Apr 30, 2012 at 17:52 UTC

    Moritz thanks again for your insight, it helped me greatly. I pushed the RegexList Movable Type plugin to github.

    The plugin is a tag modifer, meaning you place it into any Movable Type tag that outputs text. The modifier has three arguments. One specifies the substrings to process, and the other two are the "search" regex expression and the replace expression. The result of course is an array of processed substrings which are passed directly within the parent tag to a Movable type array variable (via the built-in setvar modifier). The optional fourth argument to the regex_list modifier defines an alternate capture variable (1-9) to the first match expression (the default is the whole match ($&). I've pasted the main module code below.