-4

I want to extract substring of a given string. the example string is rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;

I want to get only 0.037ms after "rta=" and percent after pl=. I tried to splite the above string by space and then by semicolon. did not work.

2

2 Answers 2

5
String s = "rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;";
Pattern pattern = Pattern.compile("rta=(.*?);.*pl=(.*?);");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. i almost wrote the same regex but missed the semicolons. it helped thanks again.
2
String str = "rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;";
String[] parts = str.split(";");
String part1 = parts[0]; // rta=0.037ms
String part2 = parts[4]; // pl=10%
...

System.out.println(part1.substring(4)); // 0.037ms
System.out.println(part2.substring(4)); // 10%

5 Comments

probably you meant String string = ... not str
@Carlos, yes.. typo mistaken :)
The second sout should be, System.out.println(part2.substring(4));
@karna, yes.. I see one space on there :)
Thanks again for this answer too. this is also the right answer. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.