简体   繁体   中英

Using java and regex to remove comma and a new line but having problems

I wrote a little demo method to try and solve this issue. I am writing some DDL statements from inspecting a database, but i cannot execute these statements because I have an extra comma at the end of each section:

I want my data to look like this:

ALTER TABLE my_evolv.account_type 
    ADD CONSTRAINT FOREIGN KEY (account_type_id) REFERENCES account_type (account_type_id)ON DELETE CASCADE ON UPDATE CASCADE,
    ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE

ALTER TABLE my_evolv.actions_library 
    ADD CONSTRAINT FOREIGN KEY (actions_library_id) REFERENCES actions_library (actions_library_id)ON DELETE CASCADE ON UPDATE CASCADE,
    ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE

ALTER TABLE my_evolv.actions_library_parameters 

and so on. This is my code:

private static void removeComma(){

    String row1 =       
        "ALTER TABLE my_evolv.account_type \n"+
        "   ADD CONSTRAINT FOREIGN KEY (account_type_id) REFERENCES account_type (account_type_id)ON DELETE CASCADE ON UPDATE CASCADE,\n"+
        "   ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE,\n";
    String row2 = 
        "ALTER TABLE my_evolv.actions_library \n"+
        "   ADD CONSTRAINT FOREIGN KEY (actions_library_id) REFERENCES actions_library (actions_library_id)ON DELETE CASCADE ON UPDATE CASCADE,\n"+
        "   ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE,\n";
    String row3 = 
        "ALTER TABLE my_evolv.actions_library_parameters ";     

    StringBuilder s = new StringBuilder();
    s.append(row1);
    String newVal = s.toString();
    newVal.replaceAll("(,\nALTER)", "\nALTER");
    System.out.println("row1:"+ newVal.toString());
    s = new StringBuilder(newVal);


    s.append(row2);
    newVal = s.toString();
    newVal.replaceAll("(,\nALTER)", "\nALTER");
    System.out.println("row2:"+ newVal.toString());
    s = new StringBuilder(newVal);

    s.append(row3);
    newVal = s.toString();
    newVal.replaceAll("(,\nALTER)", "\nALTER");
    System.out.println("row3:"+ newVal.toString());
    s = new StringBuilder(newVal);

}

If you don't have commas anywhere but at the end of the Strings then following will do the trick

newVal = newVal.replaceAll(",", "");

This will replace all the commas in the String.

Also, If you really want to use StringBuilder to generate final String, use:

StringBuilder s = new StringBuilder((row1+row2).replaceAll(",", ""));
s.toString()

This is much cleaner.

You can fix this issue with help of following regex:

,\s+$

Code:

String row1 =
        "ALTER TABLE my_evolv.account_type \n"+
                "   ADD CONSTRAINT FOREIGN KEY (account_type_id) REFERENCES account_type (account_type_id)ON DELETE CASCADE ON UPDATE CASCADE,\n"+
                "   ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE,\n";

String regex = ",\\s+$";

// Create a Pattern object
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(row1);

String result = matcher.replaceAll("");

System.out.println(result);

It will print:

ALTER TABLE my_evolv.account_type 
   ADD CONSTRAINT FOREIGN KEY (account_type_id) REFERENCES account_type (account_type_id)ON DELETE CASCADE ON UPDATE CASCADE,
   ADD CONSTRAINT FOREIGN KEY (update_log_id) REFERENCES update_log (update_log_id)ON DELETE CASCADE ON UPDATE CASCADE

As you can see it has removed last comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM