import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^[^\\\\]*)\\\\t([^\\\\]*)\\\\t([^\\\\]*)\\\\t([^\\\\]*)\\\\t([^\\\\]*)(.*)";
final String string = "19-22\\t\\t4\\tP,G\\tDOB_TT\\t\\tTime of Birth\\t\\t126\\t \\t0000-2359 Time of Birth\n"
+ "24-25\\t\\t2\\tT,G\\tOSTATE\\t\\tOccurrence Postal State\n"
+ "85-86\\t\\t2\\tG\\tMRCNTRY\\tMother’s Residence Country\\t\\tAA-ZZ\\tSee Geographic Documentation\n"
+ "87-88\\t\\t2\\tG\\tXMRSTATE\\tExpanded State of Residence of Mother\\t\\t\n"
+ "108-109\\t\\t2\\tP,G\\tMRACE_R15\\tMother’s Race Recode 15\\t\\t\\t\n"
+ "151-152\\t\\t2\\tP,G\\tFRACE_R31\\tFather’s Race Recode 31\\t\\t\\t01\\tWhite (only) [only one race reported]";
final String subst = "\\5,\\1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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