0
votes

Can we do pattern matching in RXJava similar to other functional language?

Thanks, Sumanta

1

1 Answers

0
votes

I think that we can use boolean java.lang.String.matches(String regex) applied with filter() operator; for example supposing that we need to count how many matches of a specific PATTERN there are in a list of words, we can do in this way:

private static final String PATTERN = "[q].*[A-Za-z]";

List<String> words = Arrays.asList("the","quick","brown","fox","jumped","over","the","lazy","dog");

Flowable.fromIterable(words)
        .filter(elem->elem.matches(PATTERN))
        .count()
        .subscribe(System.out::println);

And you get:

1