2

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

4
  • Hey.. the same thing I have achieved without using Java-8. I have also mentioned the function. What I want is to achieve same thing using Java-8 like using Streming etc. Commented Sep 25, 2017 at 7:36
  • See ideone.com/ChfiPj Commented Sep 25, 2017 at 7:37
  • Also possibly helpful: how can I implement a switch statement using streams? Commented Sep 25, 2017 at 7:46
  • Hi Holger, no the question you have mentioned is all together a different case. Commented Sep 25, 2017 at 8:38

1 Answer 1

4

That's available in java-9 via Scanner#findAll:

    String test = "(A,1),(Y,4),(F,5)";
    Scanner sc = new Scanner(test);
    Pattern filerRegx = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");

    List<String> results = sc.findAll(filerRegx)
            .map(mr -> mr.group(1))
            .filter(Objects::nonNull)
            .collect(Collectors.toList());

    System.out.println(results);

EDIT I have no idea how that duplicate answer does not answer your question. There's like a very simple change you need to make:

  static final class MatchItr extends Spliterators.AbstractSpliterator<String> {
    private final Matcher matcher;

    MatchItr(Matcher m) {
        super(m.regionEnd() - m.regionStart(), ORDERED | NONNULL);
        matcher = m;
    }

    @Override
    public boolean tryAdvance(Consumer<? super String> action) {
        if (!matcher.find()) {
            return false;
        }

        if (matcher.group(1) == null) {
            return false;
        }

        action.accept(matcher.group(1));
        return true;
    }
}

And use it:

MatchItr mIter = new MatchItr(regexMatcher);
StreamSupport.stream(mIter, false)
            .forEach(System.out::println);
Sign up to request clarification or add additional context in comments.

9 Comments

When the source is just a single String, you may use just Matcher.results(). PS: it’s interesting that you managed to post an answer to an already closed question…
@Holger I think it was nano seconds before it got closed
Well, it now displays a gap of seven minutes.
@Holger that's very weird! After I pressed submit, I immediately saw closed
Hi Eugene, Thanks for the answer but is it possible using Java 8?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.