All Implemented Interfaces:
android.os.Parcelable

public final class RegexParser extends Parser<RegexParser.Result>
Represents a parser which parses OCR result according to given regular expression. Regular expression parsing is not performed with java's regex engine. Instead, it is performed with custom regular expression engine which can work with multiple char recognition alternatives (if enabled in AbstractOCREngineOptions). Due to differences between parsing normal strings and OCR results, this parser does not support some regex features found in java's regex engine, like back references. See setRegex(String) for more information about what is supported.
  • Field Details

    • CREATOR

      @NonNull public static final android.os.Parcelable.Creator<RegexParser> CREATOR
  • Constructor Details

    • RegexParser

      public RegexParser(@NonNull String regex)
      Constructor using default OCR engine options and provided regular expression. Since default OCR engine options does not have character whitelist, the regex alphabet is empty - this means that '.' metacharacter which represents any character is not supported. To use regular expressions that contain this metacharacter, use RegexParser(String, AbstractOCREngineOptions) constructor. For more information about regex support, see setRegex(String).
      Parameters:
      regex - Regular expression.
      Throws:
      IllegalArgumentException - if regular expression cannot be compiled
    • RegexParser

      public RegexParser(@NonNull String regex, @NonNull AbstractOCREngineOptions ocrEngineOptions)
      Constructor using provided OCR engine options and provided regular expression. Some regex metacharacters, like '.' (represents any character) may not be supported if provided OCR engine options do not have character whitelist. For more information about regex support, see setRegex(String).
      Parameters:
      regex - Regular expression.
      ocrEngineOptions - OCR engine options used for performing OCR.
      Throws:
      IllegalArgumentException - if regular expression cannot be compiled
  • Method Details

    • setRegex

      public void setRegex(@NonNull String regex)
      Defines regex that will be used to parse OCR data. Note that not all java regex features are available, such as back references and '^' and '$' meta-character. '.' meta-character that represents any character and '^' inside brackets representing all characters except those in brackets are available only if alphabet is defined.
      Parameters:
      regex - Regular expression to be used for parsing OCR result.
      Throws:
      IllegalArgumentException - if regular expression cannot be compiled
    • getRegex

      @NonNull public String getRegex()
      Returns the currently set regular expression.
      Returns:
      the currently set regular expression.
    • setOcrEngineOptions

      public void setOcrEngineOptions(@NonNull AbstractOCREngineOptions ocrEngineOptions)
      Sets the OCR engine options used in Regex OCR parser. By default, this is set to 'default instance of BlinkOCREngineOptions'.
      Parameters:
      ocrEngineOptions - OCR engine options used in Regex OCR parser
    • getOcrEngineOptions

      @NonNull public AbstractOCREngineOptions getOcrEngineOptions()
      Returns the OCR engine options used in Regex OCR parser. By default, this is set to 'default instance of BlinkOCREngineOptions'.
      Returns:
      OCR engine options used in Regex OCR parser
    • setUseSieve

      public void setUseSieve(boolean useSieve)
      Enable the usage of algorithm for combining consecutive OCR results between video frames for improving OCR quality. By default this is turned off. Note: This option works together only with if instance of BlinkOCREngineOptions is given to setOcrEngineOptions(AbstractOCREngineOptions). Otherwise, it will not be enabled and IllegalArgumentException will be thrown.
      Parameters:
      useSieve - whether or not sieve algorithm should be used
    • shouldUseSieve

      public boolean shouldUseSieve()
      Returns true if algorithm for combining consecutive OCR results between video frames for improving OCR quality is used.
      Returns:
      true if sieve algorithm is used
    • setStartWithWhitespace

      public void setStartWithWhitespace(boolean mustStartWithWhitespace)
      If set to true, regex will not be matched if there is no whitespace before matched string. Whitespace is not returned in parsed result.
      Parameters:
      mustStartWithWhitespace - whether or not whitespace is required before matched string
    • shouldStartWithWhitespace

      public boolean shouldStartWithWhitespace()
      Returns whether matched string must start with whitespace.
      Returns:
      whether matched string must start with whitespace.
    • setEndWithWhitespace

      public void setEndWithWhitespace(boolean mustEndWithWhitespace)
      If set to true, regex will not be matched if there is no whitespace after matched string. Whitespace is not returned in parsed result.
      Parameters:
      mustEndWithWhitespace - whether or not whitespace is required after matched string
    • shouldEndWithWhitespace

      public boolean shouldEndWithWhitespace()
      Returns whether matched string must end with whitespace.
      Returns:
      whether matched string must end with whitespace.
    • terminateNative

      protected void terminateNative(long nativeContext)
      Specified by:
      terminateNative in class Entity<RegexParser.Result>
    • serializeNativeSettings

      @Nullable protected byte[] serializeNativeSettings()
      Specified by:
      serializeNativeSettings in class Entity<RegexParser.Result>
    • deserializeNativeSettings

      protected void deserializeNativeSettings(@NonNull byte[] serializedSettings)
      Specified by:
      deserializeNativeSettings in class Entity<RegexParser.Result>
    • clone

      @NonNull public RegexParser clone()
      Description copied from class: Entity
      Create a copy of the entity. This is required if properties of the entity must be modified while background thread performs processing. In order to avoid race conditions, changing properties is not allowed while entity is used in background processing. However, you are able to make a copy of the entity, change properties of it and then update the RecognizerRunnerView or RecognizerRunner with the updated entity hierarchy.
      Specified by:
      clone in class Entity<RegexParser.Result>
      Returns:
      a copy of the entity
    • consumeResultFrom

      public void consumeResultFrom(@NonNull Entity other)
      Description copied from class: Entity
      Consume result from other entity. The result from other will be put instead current result of this entity. This method is used inside BaseIntentTransferable.loadFromIntent(Intent) and similar methods to replace the result content of the saved entities with results that have arrived over Intent
      Specified by:
      consumeResultFrom in class Entity<RegexParser.Result>
      Parameters:
      other - Entity from which result should be consumed.
    • consumeResult

      public void consumeResult(@NonNull RegexParser.Result resultToConsume)
      Description copied from class: Parser
      Consumes given result. The given result will be put instead current result of this entity.
      Specified by:
      consumeResult in class Parser<RegexParser.Result>
      Parameters:
      resultToConsume - Result that should be consumed.
    • clearResult

      public void clearResult()
      Description copied from class: Parser
      Clears parser result.
      Specified by:
      clearResult in class Parser<RegexParser.Result>