Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Contextual find and replace large config file

by Veltro (Hermit)
on Jan 03, 2019 at 10:57 UTC ( #1227960=note: print w/replies, xml ) Need Help??


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:

  • Why (?:\z|\n) and not just \z when \z is 'up to and including \n'
  • Why \h*\n* and not \s*

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")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-09-27 03:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?