in reply to How to match and transform a multiline pattern?

Update: Corrected and simplified.

Based upon the single example you've provided this will need modifications, but it might give you a starting point. The basic assumption it makes are that functions in your java code always end with a single '}' in the first column of an otherwise blank line. It uses that to read the file one function at a time and performs the modifications.

As coded, the regex is pretty sensetive to variations in the formating of the source code. This can be loosened as appropriate, but increases the risk of false substitutions. It attempts to retain the original indentation, but it may not work in all situations.

#! perl -slw use strict; $/ = "\n}\n"; while( <DATA> ) { s[ ( nodeName.equals \( \s* " ( [^"]+ ) " ( .+? ) #" new \s+ ( Integer | Double ) \( .+? \)\)\); ) ][msg.isSetField(${2}.FIELD${3}(msg.get$4(${2}.FIELD));]sgx; print; } __DATA__ 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))); } } 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))); } } 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))); } }

Outputs:

C:\test>junk1 public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } } public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } } public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } }

Which is just 3 repetitions of the one example.

I'm not sure I would bother to code a filter for a single 2000 line sourcefile. I could almost certainly do that much more quickly in my editor with a macro or two--or even manually--but its fun to try :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: How to match and transform a multiline pattern?
by nbhatia (Novice) on Apr 20, 2007 at 19:39 UTC
    Thanks. This works great! I must say that I do not understand your regular expression yet, I only know the s/.../.../ variety. Haven't seen the "s[][]" variety yet. I guess I need to read up on regex now :-).

    Thanks again.
      I only know the s/.../.../ variety. Haven't seen the "s[][]" variety yet.

      They are the same, they just use different delimiters. You can choose which character(s) you use as a delimiter. If you use bracketing delimiters (eg. () or {} or [] or <>) then it can simplify the use of somethings. See the docs for the full skinny.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      See perlretut and perlre for a start. If you are new to Perl then have a good browse around the Tutorials section here too.

      A small nomenclature note: the "regular expression" is the bit that does the matching. In a substitution it is the first expression. In a match it is the only expression. In a substitution the other expression is called the substitution expression.

      For complicated regular expressions it is often helpful to use the /x switch which allows you to use white space and comments in your expression so that it is easier to see the different parts and to provide comments for the parts of the expression.


      DWIM is Perl's answer to Gödel
        Thanks so much - great information. I am ready to dive in!