in reply to Re: Contextual find and replace large config file
in thread Contextual find and replace large config file

This is great stuff haukex

I think that using Regexp::Grammars is probably the best solution, however I am getting this YACC feeling over me and think this kind of thing is programming on an entire different level. So currently I am looking at your second approach which I think will offer me the flexibility that I am looking for.

Actually I think this will help me to take this even one step further and build a more advanced configuration which will allow me to specify a filter and formulas to act on parameters. And for this I am thinking in the same lines as LanX (using a cache, separate functionality in functions etc. etc.).

I understand about 95% of the code, but I am still struggling with some of the regex items which are:

Thanks for your elaborate post

Replies are listed 'Best First'.
Re^3: Contextual find and replace large config file
by haukex (Bishop) on Jan 03, 2019 at 12:40 UTC
    Why (?:\z|\n) and not just \z when \z is 'up to and including \n'

    Not quite, \z only ever matches at the very end of the string, whereas \Z also matches before the newline at the end of the string, and the meaning of $ is changed by the /m modifier to match before every newline or at the end of the string. When I want to express "match up to the end of this line", I sometimes prefer (?:\z|\n) over $+/m because the former explicitly consumes the \n.

    Why \h*\n* and not \s*

    Because /\s*/ would also match e.g. \t\n\t, which causes a following /^.../ to no longer match, since /\s*/ consumed the \t at the beginning of the line.

    Update: Regarding the first point:

    $ perl -MData::Dump -e 'dd split /($)/m, "x\ny\nz"' ("x", "", "\ny", "", "\nz") $ perl -MData::Dump -e 'dd split /(\z|\n)/m, "x\ny\nz"' ("x", "\n", "y", "\n", "z")