Skip to content

how about matchers for RXJavaString? #27

Closed
@ArekCzarnik

Description

@ArekCzarnik

I try to work with matchers over long string files... and experiment with this implementation.

example could be:

Pattern pattern = Pattern.compile("(<div id=\"(?<id>.*)\" data-template=\"(?<template>.*)\" data-src=\"(?<src>.*)\">(?<fallback>.*)<\\/div>)");

String content = "blabla....";

Observable<Map> matcherMap = StringObservable.matcher(content, pattern,"id","template","src","fallback");

Implantation:


public static Observable<Map> matcher(final Observable<String> src, final Pattern pattern, final String... names) {
        return src.lift(new Operator<Map, String>() {
            @Override
            public Subscriber<? super String> call(final Subscriber<? super Map> subscriber) {

                return new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        if (!subscriber.isUnsubscribed())
                            subscriber.onCompleted();
                    }

                    @Override
                    public void onError(final Throwable e) {
                        if (!subscriber.isUnsubscribed())
                            subscriber.onError(e);
                    }

                    @Override
                    public void onNext(final String segment) {
                        final Matcher matcher = pattern.matcher(segment);
                        while (!subscriber.isUnsubscribed() && matcher.find()) {
                            Map matcherMap = new HashMap();
                            for (String name : Arrays.asList(names)) {
                               matcherMap.put(name,matcher.group(name));
                            }
                            subscriber.onNext(matcherMap);
                        }
                    }
                };
            }
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions