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

Atul
  • 3,043
  • 27
  • 39
  • 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. – Atul Sep 25 '17 at 07:36
  • See https://ideone.com/ChfiPj – Wiktor Stribiżew Sep 25 '17 at 07:37
  • Also possibly helpful: [how can I implement a switch statement using streams?](https://stackoverflow.com/q/37479217/2711488) – Holger Sep 25 '17 at 07:46
  • Hi Holger, no the question you have mentioned is all together a different case. – Atul Sep 25 '17 at 08:38

1 Answers1

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);
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • 1
    When the source is just a single `String`, you may use just [`Matcher.results()`](http://download.java.net/java/jdk9/docs/api/java/util/regex/Matcher.html#results--). PS: it’s interesting that you managed to post an answer to an already closed question… – Holger Sep 25 '17 at 07:55
  • @Holger I think it was nano seconds before it got closed – Eugene Sep 25 '17 at 07:55
  • 1
    Well, it now displays a gap of seven minutes. – Holger Sep 25 '17 at 07:58
  • @Holger that's very weird! After I pressed submit, I immediately saw closed – Eugene Sep 25 '17 at 07:58
  • Hi Eugene, Thanks for the answer but is it possible using Java 8? – Atul Sep 25 '17 at 08:35
  • @Atul that's what the duplicate question/answer does... – Eugene Sep 25 '17 at 08:37
  • Hi Eugene, I am seeking help to correct my answer only, which I mentioned in the post. I have to use Java 8 compiler and the answer provided as duplicate are not relevant. – Atul Sep 25 '17 at 08:40
  • @Atul see edited answer – Eugene Sep 25 '17 at 10:36
  • Hi Eugene, Thank you very much for your response. :) – Atul Sep 25 '17 at 11:06