Git Product home page Git Product logo

rxregex's Introduction

RxRegex

Reactive version of Android Regex from java.util.regex. This lib is useful if you work with large text.

The library java.util.regex has very narrow functionality. There is no way to stop the running process. You can not get the already processed part of the text - you should always wait for the full processing of the text. There is no way to get the percentage of processed text.

Features of this library:

  • Canceling parse process.
  • Get parse progress.
  • Positions of matched parts of text.
  • Positions of replaced parts of text.
  • Callback for each parsed part.
  • Support \n\r\t in replacement.

Exists two base classes: RxRegex - Reactive version and Regex - Callback version.

Add dependencies

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    compile 'com.github.tepikin:RxRegex:0.1'
}

How to use RxRegex

For create Observable use methods RxRegex.replace and RxRegex.find. It's Disposable objects and you can use it for stop parsing process, you can use method dispose() or just unsubscribe from Observable and parsing stops automatically.

Srting input = "Long text !12! for parsing !AB!";    // The character sequence to be matched
Srting regex = "!..!";                               // Regular expression
Srting replacement = "ABCD";                         // replacement text


// Simple usage
RxRegex.replace(input, regex, replacement)      
    .subscribe(replace -> log(replace.toString()));  // logs out: 
                                                     //    Long text   -> Long text
                                                     //    !12!        -> ABCD
                                                     //    for parsing -> for parsing
                                                     //    !AB!        -> ABCD


// Stop parsing progess by dispose()
Disposable disposable = RxRegex.replace(input, regex, replacement)
    .filter(RxRegex.OnAppend::isMatched)             // filter parts matched to regex
    .subscribe(replace -> log(replace.toString()));  // logs out:
                                                     //    !12! -> ABCD
                                                     //    !AB! -> ABCD
disposable.dispose();  // you can stop parsing at any time.


// Object in onNext() is RxRegex.onAppend. It's fields description below.
RxRegex.replace(input, regex, replacement)      
    .subscribe(replace -> {
        replace.getFromSrc()         // Start position of current part at original text
        replace.getToSrc()           // End position of current part at original text
        replace.getAppendSrc()       // Cuttent processed text part from original text
        replace.getFromDst()         // Start position of current part at replaced text
        replace.getToDst()           // End position of current part at replaced text
        replace.getAppendDst()       // Replaced text part
        replace.isMatched()          // Is current part matched to regex
        replace.getProgress()        // Current parsing progress (float from 0 - to 1)
        replace.getMatchedCount()    // Count of matched perts at this moment
    });      
    
    
// Use reactive with threads
RxRegex.replace("abcd", "bc", "BC", 0)
 .subscribeOn(Schedulers.computation())        // Parse in computation thread
 .observeOn(AndroidSchedulers.mainThread())    // Observe in Android mainThread
 .scan(new StringBuffer(), (stringBuffer, onAppend) -> stringBuffer.append(onAppend.getAppendDst())).skip(1)
 .last(new StringBuffer()).map(stringBuffer -> stringBuffer.toString())
 .subscribe((result) -> Log.i("",result));     // result == "aBCd"

How to use Non reactive version

Non reactive version work with class Regex.

// Simple usage
String result = Regex.replace("abcd", "bc", "BC");  // result = "aBCd" 

// Use listener
Regex.replace("abcd", "bc", "BC", 0, listener );    // calls Listener with args "a -> a", "bc -> BC", "d -> d"

// Use CancelationSignal
CancelationSignal cancelationSignal = new CancelationSignalImpl();
Regex.replace("abcd", "(bc)", "_$1_", 0, listener, cancelationSignal ); 
cancelationSignal.cancel();                         // cancelationSignal stop parsing process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.