import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([^\\s:\\n][^:\\n]*)\\s+\\:\\s+\\[\\s*([^][]*)\\s+]";
final String string = "Date : [ 2010-01-01 XX:XX:XX ] Age : [ 22 ] Sex : [ M ] : [ XXX ]\n"
+ "Height(cm) : [ 145 ] Weight(kg) : [ 56.4 ] Race : [ Hispanic ]\n"
+ "Spirometry : [ restrictive pattern ]\n"
+ "Treatment response : [ Negative ]\n"
+ "Tissue volume : [ Normal ]\n"
+ "Tissue volume\n"
+ "[ Normal RV ] \n"
+ "Diffusing capacity : [ Normal capacity ]\n"
+ "FVC Liters : [ 2.22 ] FVC Liters : [ 67 ] FVC Liters : [ 3.35 ] \n"
+ "FEV1 Liters : [ 1.96 ] FEV1 Liters : [ 66 ] FEV1 Liters : [ 2.06 ] \n"
+ "FEV1 / FVC % : [ 58 ] FEV1 / FVC % : [ 62 ]\n"
+ "DLCO mL/mmHg/min : [ 21.5 ] DLCO mL/mmHg/min : [ 102 ]\n"
+ "DLCO Adj mL/mmHg/min : [ 21.5 ] DLCO Adj mL/mmHg/min : [ 102 ]\n"
+ "RV/TLC % : [ 22 ]";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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