Any improvements are welcome.


sed -r "s/private (.*) (.*);(.*)/&\npublic \1 get\u\2(){return \2;}\npublic void set\u\2(\u\2){this.\2=\u\2;}\n/" "%~1">"%~2"


private String firstName;// Stores firstName
private String lastName;
private String address;

using a.bat in out


private String firstName;// Stores firstName
public String getFirstName(){return firstName;}
public void setFirstName(FirstName){this.firstName=FirstName;}

private String lastName;
public String getLastName(){return lastName;}
public void setLastName(LastName){this.lastName=LastName;}

private String address;
public String getAddress(){return address;}
public void setAddress(Address){this.address=Address;}

To do: add javadoc generation to methods.


closed as off-topic by Jamal Jul 28 '15 at 6:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ Is this an exercise? Just because most IDEs do this out of the box. Otherwise I think it's an elegant solution :) \$\endgroup\$ – c_maker Apr 20 '12 at 1:36
  • \$\begingroup\$ Thanks. I didn't really look into the IDE, since I use mostly a separate editor. \$\endgroup\$ – user93200 Apr 20 '12 at 2:13
  • \$\begingroup\$ private String firstName;// Stores firstName You may know it, I say it anyway...do not comment what is obvious. ;) \$\endgroup\$ – Bobby Apr 20 '12 at 9:03

You forgot the parameter type in the setter methods (including @blufox suggestion):

sed -r "s/private\s+([^; ]+)\s+([^; ]+)\s*;(.*)$/&\npublic \1 get\u\2(){return \2;}\npublic void set\u\2(\1 \2){this.\2=\u\2;}\n/" "%~1">"%~2"

which produces:

private String firstName;// Stores firstName
public String getFirstName(){return firstName;}
public void setFirstName(String firstName){this.firstName=FirstName;}

But: you better use an IDE (e.g., Eclipse) for these tasks. You are trying to parse Java code with a regex which is a bad idea.

Just some examples:

  • declarations could be on different lines

    private String // some comment
        // some other comment
        ; // semicolon
  • the public or private modifier could be omitted

    String myString;
  • fields can be initialized

    String myString = "my string";
  • fields can be declared final

    private final String myString;
  • and so on


s/private (.*) (.*);(.*) This may be better done as s/private ([^; ]+) +([^; ]+) *;(.*)$

This avoids matching semicolon and space in the type and variable name. (Be as restrictive in your match as you can.)


Not the answer you're looking for? Browse other questions tagged or ask your own question.