I have created a function which return the List. I am passing a string "(A,1),(Y,4),(F,5)" and I want to split it out based on brackets to get the 3 individual string object which are like A,1, Y,4, F,5 as 3 individual objects in a list. I used Java 8 to create it but it return me only one value like A,1 The function is:
private List<String> getDataOnRegx(String str) {
Pattern filerRegx = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");
Matcher regexMatcher = filerRegx.matcher(str);
return Stream.of(str).
filter(s -> regexMatcher.find() && regexMatcher.group(1) != null).
map(r -> new String(regexMatcher.group(1))).
collect(Collectors.toList());
}
The expected result I have achieved via below function where I have not used any Java 8 feature:
private List<String> getDataOnRegx(String str) {
Pattern regex = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");
Matcher regexMatcher = regex.matcher(str);
List<String>dataList = new ArrayList<String>();
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
dataList.add(regexMatcher.group(1));
}
}
System.out.println(dataList);
return dataList;
}
Can somebody help me to get the all objects in a list. Just want help to correct my function which I already written using Java 8. Strictly I have to use Java 8 compiler.
Thanks, Atul