import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(.*),(.*),(.*),(.*)";
final String string = ",UBS154754528,A121215441,10\n"
+ ",UBS154754528,A121215442,20\n"
+ ",UBS154754528,A121215443,15\n"
+ "GFdfgdfD45454dfgd5457,UBS154754528,A121215444,25\n"
+ "AAA454545455,UBS154754528,A121215445,254\n"
+ "vvGFD454545457,UBS154754528,A121215446,215\n"
+ "dad454545457,UBS154754528,A121215447,55\n"
+ "eww454545457,UBS154754528,A121215448,214\n"
+ "asd454545457,UBS154754528,A121215449,26\n"
+ "trtr454545457,UBS154754528,A121215450,25\n"
+ ",UBS154754528,A121215451,251\n"
+ ",UBS154754528,A121215452,252\n"
+ ",UBS154754528,A121215453,23\n"
+ ",UBS154754528,A121215454,251\n"
+ "gdfgd454545457,UBS154754528,A121215455,251\n"
+ "dfdf454545457,UBS154754528,A121215456,25\n"
+ "fgdfgd454545457,UBS154754528,A121215457,95\n"
+ "gdfgFD45dfg4545457,UBS154754528,A121215458,58\n"
+ "dfgdf4545fg45457,UBS154754528,A121215459,254\n"
+ "dfgd45454dfgfd5457,UBS154754528,A121215460,25\n"
+ "dfdfGFD45dfgdf4545457,UBS154754528,A121215461,254\n"
+ ",UBS154754528,A121215462,66\n"
+ ",UBS154754528,A121215463,22\n"
+ ",UBS154754528,A121215464,54\n"
+ "GfdfgD454fd545457,UBS154754528,A121215465,24\n"
+ "GFfdgD454dfg545457,UBS154754528,A121215466,45\n"
+ "GFfdgD45454fgd5457,UBS154754528,A121215467,11\n"
+ "fgfD4545dfg45457,UBS154754528,A121215468,255\n"
+ "GFfdgD454dfgd545457,UBS154754528,A121215469,88\n";
final String subst = "insert into test_table values('$1','$2','$3',$4);";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html