import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "((^0)\\s+|(^1)\\s+|(^2)\\s+|(^3)\\s+|(^4)\\s+|(^5)\\s+)((((.{4})(.+)))|((.{4})))";
final String string = "0 @I0066@ INDI\n"
+ "1 NAME Joan Adrienne /Hatch/\n"
+ "2 GIVN Joan Adrienne\n"
+ "2 SURN Hatch\n"
+ "2 SOUR @S0045@\n"
+ "3 DATA\n"
+ "4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota\n"
+ "2 SOUR @S0131@\n"
+ "3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A\n"
+ "3 DATA\n"
+ "4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot\n"
+ "5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis\n"
+ "5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch\n"
+ "3 NOTE @N0121@\n"
+ "2 SOUR @S0635@\n"
+ "3 DATA\n"
+ "4 TEXT Marriage date: 29 Dec 1953 Marriage place: Cook County, IL\n"
+ "1 SEX F\n"
+ "1 BIRT\n"
+ "2 DATE 30 MAY 1935\n"
+ "2 PLAC Minneapolis, Hennepin, Minnesota, USA\n"
+ "2 SOUR @S0045@\n"
+ "3 DATA\n"
+ "4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota\n"
+ "2 SOUR @S0131@\n"
+ "3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A\n"
+ "3 DATA\n"
+ "4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot\n"
+ "5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis\n"
+ "5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch\n"
+ "3 NOTE @N0125@\n"
+ "1 DEAT\n"
+ "2 DATE 14 DEC 1989\n"
+ "2 PLAC Nyack, Rockland, New York, USA\n"
+ "1 RESI\n"
+ "2 DATE 1 APR 1940\n"
+ "2 PLAC Faribault, Rice, Minnesota, USA\n"
+ "2 SOUR @S0131@\n"
+ "3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A\n"
+ "3 DATA\n"
+ "4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot\n"
+ "5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis\n"
+ "5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch\n"
+ "3 NOTE @N0127@\n"
+ "1 RESI\n"
+ "2 DATE 1 APR 1940\n"
+ "2 PLAC Faribault, Rice, Minnesota, USA\n"
+ "2 SOUR @S0131@\n"
+ "3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A\n"
+ "3 DATA\n"
+ "4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot\n"
+ "5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis\n"
+ "5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch\n"
+ "3 NOTE @N0129@\n"
+ "1 FAMC @F1196@\n"
+ "1 FAMS @F0129@\n"
+ "1 FAMS @F0260@\n"
+ "1 CHAN\n"
+ "2 DATE 31 MAR 2017\n"
+ "3 TIME 22:03:40";
final String subst = "\\n $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 | $10 | $11 | $12 | $13 | $14 | |";
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