I am trying to translate a large java file - approximately 2000 lines of code. I need to replace all calls to a legacy API to a newer API. Essentially, I need to match patterns spanning multiple lines and transform them into new patterns. Anything that does not match these patterns should pass through without any change. For example:
Original file ============= public Product getProduct(Node node) { // --- bunch of logic here --- if (nodeName.equals("ItemNum")) { product.setItemNumber( new Integer(StringUtils.parseInt(nodeValue, 0))); } if (nodeName.equals("AvgPrice")) { product.setAveragePrice( new Double(StringUtils.parseDouble(nodeValue, 0.0))); } } Converted file ============== public Product getProduct(Node node) { // --- bunch of logic here --- if (msg.isSetField(ItemNum.FIELD)) { product.setItemNumber(msg.getInt(ItemNum.FIELD)); } if (msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice(msg.getDouble(AvgPrice.FIELD)); } }
I just picked up Perl yesterday (thanks to perlintro) and trying to figure out the best approach to do this. My first attempt uses regular expressions one line at a time – if it matches /nodeName.equals/, then it tries to match the following two lines, a) to pick up the method name for the “product” call and b) to make sure that the type is Integer or Double (there can be other types with different patterns). Here’s my Perl code:
while (<STDIN>) { if ($_ =~ /nodeName.equals/) { my $line1 = $_; my $line2 = <STDIN>; my $line3 = <STDIN>; if ($line3 =~ /new Integer/) { chomp($line1); $_ = "$line1$line2"; s/nodeName.equals\("(.*)"\).*product\.(.*)\(/msg.isSetFiel +d($1.FIELD)) {\n product.$2(msg.getInt($1.FIELD))\;/; print $_; } elsif ($line3 =~ /new Double/) { chomp($line1); $_ = "$line1$line2"; s/nodeName.equals\("(.*)"\).*product\.(.*)\(/msg.isSetFiel +d($1.FIELD)) {\n product.$2(msg.getDouble($1.FIELD))\;/; print $_; } else { print $line1; print $line2; print $line3; } } else { print $_; } }
Is there a better way?
1) Can I somehow match and convert the entire four line block with one substitution?
2) Is regex the best tool to do this, or should I be looking at something else?

Thanks for your help.


In reply to How to match and transform a multiline pattern? by nbhatia

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.