org.apache.lucene.search

## Class RegexpQuery

- java.lang.Object
- - org.apache.lucene.search.Query
  - - org.apache.lucene.search.MultiTermQuery
    - - org.apache.lucene.search.AutomatonQuery
      - - org.apache.lucene.search.RegexpQuery

- All Implemented Interfaces:
    Accountable

      public class RegexpQuery
      extends AutomatonQuery

  A fast regular expression query based on the `org.apache.lucene.util.automaton` package.
  - Comparisons are fast
  - The term dictionary is enumerated in an intelligent way, to avoid comparisons. See `AutomatonQuery` for more details.

  The supported syntax is documented in the `RegExp` class. Note this might be different than other regular expression implementations. For some alternatives with different syntax, look under the sandbox.

  Note this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow RegexpQueries, a Regexp term should not start with the expression `.*`

  See Also:
    `RegExp`
  WARNING: This API is experimental and might change in incompatible ways in the next release.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.lucene.search.MultiTermQuery

      `MultiTermQuery.RewriteMethod, MultiTermQuery.TopTermsBlendedFreqScoringRewrite, MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite, MultiTermQuery.TopTermsScoringBooleanQueryRewrite`

  - ### Field Summary

    - ### Fields inherited from class org.apache.lucene.search.AutomatonQuery

      `automaton, automatonIsBinary, compiled, term`

    - ### Fields inherited from class org.apache.lucene.search.MultiTermQuery

      `CONSTANT_SCORE_BOOLEAN_REWRITE, CONSTANT_SCORE_REWRITE, field, rewriteMethod, SCORING_BOOLEAN_REWRITE`

    - ### Fields inherited from interface org.apache.lucene.util.Accountable

      `NULL_ACCOUNTABLE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RegexpQuery(Term term)` Constructs a query for terms matching `term`.
    `RegexpQuery(Term term, int flags)` Constructs a query for terms matching `term`.
    `RegexpQuery(Term term, int syntax_flags, AutomatonProvider provider, int determinizeWorkLimit)` Constructs a query for terms matching `term`.
    `RegexpQuery(Term term, int flags, int determinizeWorkLimit)` Constructs a query for terms matching `term`.
    `RegexpQuery(Term term, int syntax_flags, int match_flags, AutomatonProvider provider, int determinizeWorkLimit)` Constructs a query for terms matching `term`.
    `RegexpQuery(Term term, int syntax_flags, int match_flags, int determinizeWorkLimit)` Constructs a query for terms matching `term`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Term` | `getRegexp()` Returns the regexp of this query wrapped in a Term.
    `String` | `toString(String field)` Prints a user-readable version of this query.

    - ### Methods inherited from class org.apache.lucene.search.AutomatonQuery

      `equals, getAutomaton, getTermsEnum, hashCode, isAutomatonBinary, ramBytesUsed, visit`

    - ### Methods inherited from class org.apache.lucene.search.MultiTermQuery

      `getField, getRewriteMethod, getTermsEnum, rewrite, setRewriteMethod`

    - ### Methods inherited from class org.apache.lucene.search.Query

      `classHash, createWeight, sameClassAs, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.lucene.util.Accountable

      `getChildResources`

- - ### Constructor Detail

    - #### RegexpQuery

          public RegexpQuery(Term term)

      Constructs a query for terms matching `term`.

      By default, all regular expression features are enabled.

      Parameters:
        `term` - regular expression.

    - #### RegexpQuery

          public RegexpQuery(Term term,
                             int flags)

      Constructs a query for terms matching `term`.
      Parameters:
        `term` - regular expression.
        `flags` - optional RegExp features from `RegExp`

    - #### RegexpQuery

          public RegexpQuery(Term term,
                             int flags,
                             int determinizeWorkLimit)

      Constructs a query for terms matching `term`.
      Parameters:
        `term` - regular expression.
        `flags` - optional RegExp syntax features from `RegExp`
        `determinizeWorkLimit` - maximum effort to spend while compiling the automaton from this regexp. Set higher to allow more complex queries and lower to prevent memory exhaustion. Use `Operations.DEFAULT_DETERMINIZE_WORK_LIMIT` as a decent default if you don't otherwise know what to specify.

    - #### RegexpQuery

          public RegexpQuery(Term term,
                             int syntax_flags,
                             int match_flags,
                             int determinizeWorkLimit)

      Constructs a query for terms matching `term`.
      Parameters:
        `term` - regular expression.
        `syntax_flags` - optional RegExp syntax features from `RegExp` automaton for the regexp can result in. Set higher to allow more complex queries and lower to prevent memory exhaustion.
        `match_flags` - boolean 'or' of match behavior options such as case insensitivity
        `determinizeWorkLimit` - maximum effort to spend while compiling the automaton from this regexp. Set higher to allow more complex queries and lower to prevent memory exhaustion. Use `Operations.DEFAULT_DETERMINIZE_WORK_LIMIT` as a decent default if you don't otherwise know what to specify.

    - #### RegexpQuery

          public RegexpQuery(Term term,
                             int syntax_flags,
                             AutomatonProvider provider,
                             int determinizeWorkLimit)

      Constructs a query for terms matching `term`.
      Parameters:
        `term` - regular expression.
        `syntax_flags` - optional RegExp features from `RegExp`
        `provider` - custom AutomatonProvider for named automata
        `determinizeWorkLimit` - maximum effort to spend while compiling the automaton from this regexp. Set higher to allow more complex queries and lower to prevent memory exhaustion. Use `Operations.DEFAULT_DETERMINIZE_WORK_LIMIT` as a decent default if you don't otherwise know what to specify.

    - #### RegexpQuery

          public RegexpQuery(Term term,
                             int syntax_flags,
                             int match_flags,
                             AutomatonProvider provider,
                             int determinizeWorkLimit)

      Constructs a query for terms matching `term`.
      Parameters:
        `term` - regular expression.
        `syntax_flags` - optional RegExp features from `RegExp`
        `match_flags` - boolean 'or' of match behavior options such as case insensitivity
        `provider` - custom AutomatonProvider for named automata
        `determinizeWorkLimit` - maximum effort to spend while compiling the automaton from this regexp. Set higher to allow more complex queries and lower to prevent memory exhaustion. Use `Operations.DEFAULT_DETERMINIZE_WORK_LIMIT` as a decent default if you don't otherwise know what to specify.

  - ### Method Detail

    - #### getRegexp

          public Term getRegexp()

      Returns the regexp of this query wrapped in a Term.

    - #### toString

          public String toString(String field)

      Prints a user-readable version of this query.
      Overrides:
        `toString` in class `AutomatonQuery`

Copyright © 2000-2024 Apache Software Foundation. All Rights Reserved.