-1

I have this string in Java:

String str = "-High Upload Observed|High| eventId=285475664495 MMTT type=2  mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= modelConfidence=0 severity=0";

From the above string I need output be like

eventId=285475664495 MMTT 
type=2 
mrt=1482650158658
in=104858769 
out=104858769 
sessionId=0 
generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= 
modelConfidence=0 
severity=0
halfer
  • 19,824
  • 17
  • 99
  • 186
Mohan M
  • 115
  • 2
  • 9
  • 1
    "can someone help me" => Of course, we can. Show us your code, tell us where it fails to get your expected output, and we will willingly help you to correct it. – Seelenvirtuose Dec 25 '16 at 12:10
  • `String str = "-High Upload Observed|High| eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= modelConfidence=0 severity=0"` need to extract the attributes `output : eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= modelConfidence=0 severity=0` – Mohan M Dec 25 '16 at 12:36
  • @Mohan Better update the question and clearly mention what are thing that are going to be same in the string (if any) – Gurwinder Singh Dec 25 '16 at 12:41
  • @Mohan do you really need MMTT at the end of the first? – Gurwinder Singh Dec 25 '16 at 12:54
  • yes that goes with the value – Mohan M Dec 25 '16 at 12:56

3 Answers3

4

Split the string into array by |, get the third element and remove everything after last space;

String s = "Office|High| eventId=285469322819 MMTT type=2";
s = s.split("\\|")[2].trim().replaceAll("[^ ]*$", "").trim();

EDIT:

Based on what OP given in the comment and assuming 'type` is always the third word.

str = str.split("\\|")[2].replaceAll("type.*", "").trim() ;

EDIT 2: Requirement changed again:

String str = "-High Upload Observed|High| eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\\=\\= modelConfidence=0 severity=0\" output : eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\\=\\= modelConfidence=0 severity=0";
Pattern p = Pattern.compile("[^ ]+=[^ ]+");
Matcher m = p.matcher(str.split("\"")[0]);
while (m.find()) {
    System.out.println(m.group());
}

produces:

eventId=285475664495
type=2
mrt=1482650158658
in=104858769
out=104858769
sessionId=0
generatorID=3+ACVIFkBABCAA951mZ0UyA\=\=
modelConfidence=0
severity=0

I admit MMTT is missing in the first one, but oh well.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
  • String str = "-High Upload Observed|High| eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= modelConfidence=0 severity=0" output : eventId=285475664495 MMTT type=2 mrt=1482650158658 in=104858769 out=104858769 sessionId=0 generatorID=3+ACVIFkBABCAA951mZ0UyA\=\= modelConfidence=0 severity=0" – Mohan M Dec 25 '16 at 12:32
1

Here is a regex pattern that keeps the MMTT part as well:

Pattern pattern = Pattern.compile("([^ =]+)=([^ ]+(?: +[^ =]+(?= |$))*)");

Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
    System.out.println(matcher.group());
}

You can also use group(1) and group(2) to get the key and value.

Bubletan
  • 3,833
  • 6
  • 25
  • 33
0

you can get the id like this :

String str = "Office|High| eventId=285469322819 MMTT type=2";
eventId = str.split("eventId=")[1].split("type")[0].trim();
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28