String info = "ABHom=1.00;AC=2;AF=1.00;AN=2;DP=24;Dels=0.00;FS=0.000;"
+ " HaplotypeScore=0.9947;MLEAC=2;MLEAF=1.00;MQ=53.03;MQ0=0;QD=32.49;"
+ "VQSLOD=2.70; culprit=FS";
Pattern pattern = Pattern.compile("(\\w+)=(\\d+(.\\d+)?)");
Matcher matcher = pattern.matcher(info);
while (matcher.find()) {
System.out.println("key: "+matcher.group(1) +" value: "+matcher.group(2));
}
output :
key: ABHom value: 1.00
key: AC value: 2
key: AF value: 1.00
key: AN value: 2
key: DP value: 24
key: Dels value: 0.00
key: FS value: 0.000
key: HaplotypeScore value: 0.9947
key: MLEAC value: 2
key: MLEAF value: 1.00
key: MQ value: 53.03
key: MQ0 value: 0
key: QD value: 32.49
key: VQSLOD value: 2.70
explanation :
\\w mean any character include _ \\w+ means array of characters
\\d mean any digit \\d+ means array of digits
? Matches the preceding element zero or one time. For example, ab?c matches only "ac" or "abc".
you said that i want to extract string and numbers , because of this the code above can not extract culprit=FS
but if you want to extract all pair you should use this code :
Pattern pattern = Pattern.compile("(\\w+)=([^;]+)");
Matcher matcher = pattern.matcher(info);
while (matcher.find()) {
System.out.println("key: "+matcher.group(1) +" value: "+matcher.group(2));
}
output :
key: ABHom value: 1.00
key: AC value: 2
key: AF value: 1.00
key: AN value: 2
key: DP value: 24
key: Dels value: 0.00
key: FS value: 0.000
key: HaplotypeScore value: 0.9947
key: MLEAC value: 2
key: MLEAF value: 1.00
key: MQ value: 53.03
key: MQ0 value: 0
key: QD value: 32.49
key: VQSLOD value: 2.70
key: culprit value: FS