Reputation: 79
I have a long string containing different values/strings i want to extract.
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";
Matcher matcher = Pattern.compile("[A][B][h][o][m][=]([0-9]+\\.[0-9]+)").matcher(info);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
ABhom = Double.parseDouble(someNumberStr);
Matcher matcher = Pattern.compile("[M][L][E][A][C][=]/([0-9]+)").matcher(info);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
MLEAC = Integer.parseInt(someNumberStr);
I'am new to regex. Is there any smarter way to extract the numbers/strings after the equals sign ?
I'am thankful for any suggestions!
Upvotes: 2
Views: 7511
Reputation: 113
I don't think regex is a good idea. Try info.split(";")[0].split("=")[1]
with some extra boundary check.
Upvotes: 0
Reputation: 2535
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
Upvotes: 0
Reputation: 1013
You can store them in HashMap as follows:
String[] parts = info.split(";");
Map<String, String> hashMap = new HashMap<String, String>();
for (String s : parts) {
String[] keyVal = s.trim().split("=");
hashMap.put(keyVal[0], keyVal[1]);
}
and later on you may use hashMap object to get it's values.
Upvotes: 0
Reputation: 26094
You can do like this
String[] split = info.split(";");
for (String string : split) {
String[] split2 = string.trim().split("=");
System.out.println(split2[0] +" :" +split2[1]);
}
Upvotes: 0
Reputation: 79838
I think what you want to do is to turn your String
into a HashMap<String,String>
.
First, you'll need to split your string around semicolons. Then, iterate the array that you get, splitting each entry around the equals sign, and adding the result to the HashMap
.
I suggest you read about the split
method of the String
class for how to do this, and also read about the HashMap
class. Look at http://docs.oracle.com/javase/7/docs/api/java/lang/String.html and http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html and post again if you need more help.
Upvotes: 5