Tuesday, 10 September 2013

Capturing Group with regex?

Capturing Group with regex?

My input string is
String input=" 4313 :F:7222;scott miles:F:7639;Henry Gile:G:3721";
Its a string with semiclon demiliter.It can contain any number of values
selimited by semicolumn
i want to use Group capture feature in java and capture the below values
(i.e : delimited)
4313 :F:7222
scott miles:F:7639
Henry Gile:G:3721
i know i can use split function under Spring class but for some reason i
want to use group capture here.
I tried
Matcher myMatcher = Pattern.compile("(.*?);").matcher(
input);
while (myMatcher.find()) {
System.out.println("group is " + myMatcher.group());
}
output is
group is 4313 :F:7222;
group is scott miles:F:7639
;
but expected output is
group is 4313 :F:7222
group is scott miles:F:7639
group is Henry Gile:G:3721
i am not getting how to capture the last value after last semicolumn and
also iwant to get ride of semicolumn as i mentioned in expected outcome.

No comments:

Post a Comment