I have been asked to come up with a little something that could be used whenever we need to do a mass find-replace on large chunks of code. We're talking about CSTOL code that's edited in Emacs, and we need to swap out something like the below that shows up multiple times in anywhere from 1 - 10 (maybe more) different files.

; This code does something fancy enable fancy_command wait (fancy = true) or for $max_time if $$error = $time out write "Oh crap, the fancy thing didn't work!" deal with not working else do fancy_command write "fancy command done!" endif

I figured this would be do-able with regular expressions in Perl, but I have no idea how, as I've never tried to match more than a single line at a time.

So I consulted my good friend Google and found this: http://noctilucent.org/blog/2003/12/replacing-large-chunks-of-text-with-perl.html

So I gave that solution a shot. It seems to spit out a chunk of reasonable-looking regex into sub.pl, but beyond that does nothing, and I'm really not sure how the sub.pl output is supposed to work. For my example "code" above, I get this in sub.pl:

;\s+This\s+code\s+does\s+something\s+fancy\s+ enable\s+fancy_command\s+ wait\s+\(fancy\s+=\s+true\)\s+or\s+for\s+\$max_time\s+ if\s+\$\$error\s+=\s+\$time\s+out\s+ write\s+"Oh\s+crap,\s+the\s+fancy\s+thing\s+didn't\s+work!"\s+ deal\s+with\s+not\s+working\s+ else\s+ do\s+fancy_command\s+ write\s+"fancy\s+command\s+done!"\s+ endif\s+%TESTING %six

And, according to the web page, I can run that on my files (assuming similar file names) with this command: perl -p -0777 -i.bak sub.pl filename*.prc When I try that, nothing happens. My file doesn't get changed in any way (although the last modified date/time does get updated), but it does make a nice backup copy.

So, I'm lost. Can anyone help me find my way?

Kaiti
Swiss Army Nerd

In reply to Using Perl Regex to find & replace large chunks in code by jedikaiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.