Package ghidra.util

# Class UserSearchUtils

java.lang.Object
ghidra.util.UserSearchUtils

public class UserSearchUtils extends Object
This class converts user inputted strings and creates `Pattern`s from them that can be used to create `Matcher` objects. Some methods create patterns that are meant to be used with `Matcher.matches()`, while others create patterns meant to be used with `Matcher.find()`. Please see each method javadoc for clarification.

Note: methods in the class will escape regex characters, which means that normal regex queries will not work, but will be instead interpreted as literal string searches.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final Pattern`
  `NON_GLOB_BACKSLASH_PATTERN`
  A pattern that will find all '\' chars that are not followed by '*', '?' or another '\'
  `static final String`
  `STAR`
  Wildcard string for matching 0 or more characters.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `UserSearchUtils()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Pattern`
  `createContainsPattern(String input, boolean allowGlobbing, int options)`
  Creates a regular expression Pattern that will match all strings that contain the given input string.
  `static Pattern`
  `createEndsWithPattern(String input, boolean allowGlobbing, int options)`
  Creates a regular expression Pattern that will match all strings that end with the given input string.
  `static Pattern`
  `createLiteralSearchPattern(String text)`
  Generate a compiled representation of a regular expression, ignoring regex special characters .
  `static Pattern`
  `createPattern(String input, boolean allowGlobbing, int options)`
  Creates a regular expression Pattern that will match all strings that match exactly the given input string.
  `static String`
  `createPatternString(String input, boolean allowGlobbing)`
  Creates a regular expression that can be used to create a Pattern that will match all strings that match the given input string.
  `static Pattern`
  `createSearchPattern(String input, boolean caseSensitive)`
  Note: this is the default model of how to let users search for things in Ghidra.
  `static Pattern`
  `createStartsWithPattern(String input, boolean allowGlobbing, int options)`
  Creates a regular expression Pattern that will match all strings that start with the given input string.

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

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### STAR

    public static final String STAR
    Wildcard string for matching 0 or more characters.
    See Also:
      - Constant Field Values

  - ### NON_GLOB_BACKSLASH_PATTERN

    public static final Pattern NON_GLOB_BACKSLASH_PATTERN
    A pattern that will find all '\' chars that are not followed by '*', '?' or another '\'

- ## Constructor Details

  - ### UserSearchUtils

    public UserSearchUtils()

- ## Method Details

  - ### createSearchPattern

    public static Pattern createSearchPattern(String input, boolean caseSensitive)
    Note: this is the default model of how to let users search for things in Ghidra. This is NOT a tool to allow regex searching, but instead allows users to perform searches while using familiar globbing characters such as '*' and '?'.

    This method can be used with `Matcher.matches()` or `Matcher.find()`.

    Create a regular expression from the given input. Note: the regular expression created by this method is not a pure regular expression. More specifically, many regular expression characters passed to this method will be escaped (see `escapeAllRegexCharacters(String)`.

    Also, globbing characters will be changed from a regular expression meaning to a command-line style glob meaning.

    Note: This method will escape regular expression characters, such as:

    - ?
    - .
    - $
    - ...and many others
    Thus, this method is not meant to accept regular expressions, but rather generates regular expressions.
    Parameters:
      `input` - string to create a regular expression from
      `caseSensitive` - true if the regular expression is case sensitive
    Returns:
      Pattern the compiled regular expression
    Throws:
      `PatternSyntaxException` - if the input could be compiled

  - ### createLiteralSearchPattern

    public static Pattern createLiteralSearchPattern(String text)
    Generate a compiled representation of a regular expression, ignoring regex special characters . The resulting pattern will match the literal text string.

    This method can be used with `Matcher.matches()` or `Matcher.find()`.

    This method will not turn globbing characters into regex characters. If you need that, then see the other methods of this class.

    Parameters:
      `text` - search string
    Returns:
      Pattern the compiled regular expression
    Throws:
      `PatternSyntaxException` - if the input could be compiled

  - ### createStartsWithPattern

    public static Pattern createStartsWithPattern(String input, boolean allowGlobbing, int options)
    Creates a regular expression Pattern that will match all strings that start with the given input string.

    This method should only be used with `Matcher.matches()`.

    The returned regular expression Pattern should be used with the "matches" method on a Matcher. (As opposed to "find").

    Parameters:
      `input` - the string that you want to your matched strings to start with.
      `allowGlobbing` - if true, globing characters (* and ?) will converted to regex wildcard patterns; otherwise, they will be escaped and searched as literals.
      `options` - any `Pattern` options desired. For example, you can pass `Pattern.CASE_INSENSITIVE` to get case insensitivity.
    Returns:
      a regular expression Pattern that will match all strings that start with the given input string.

  - ### createEndsWithPattern

    public static Pattern createEndsWithPattern(String input, boolean allowGlobbing, int options)
    Creates a regular expression Pattern that will match all strings that end with the given input string.

    This method should only be used with `Matcher.matches()`.

    The returned regular expression Pattern should be used with the "matches" method on a Matcher. (As opposed to "find").

    Parameters:
      `input` - the string that you want to your matched strings to end with.
      `allowGlobbing` - if true, globing characters (* and ?) will converted to regex wildcard patterns; otherwise, they will be escaped and searched as literals.
      `options` - any `Pattern` options desired. For example, you can pass `Pattern.CASE_INSENSITIVE` to get case insensitivity.
    Returns:
      a regular expression Pattern that will match all strings that end with the given input string.

  - ### createContainsPattern

    public static Pattern createContainsPattern(String input, boolean allowGlobbing, int options)
    Creates a regular expression Pattern that will match all strings that contain the given input string.

    This method should only be used with `Matcher.matches()`.

    Parameters:
      `input` - the string that you want to your matched strings to contain.
      `allowGlobbing` - if true, globing characters (* and ?) will converted to regex wildcard patterns; otherwise, they will be escaped and searched as literals.
      `options` - any `Pattern` options desired. For example, you can pass `Pattern.CASE_INSENSITIVE` to get case insensitivity.
    Returns:
      a regular expression Pattern that will match all strings that contain the given input string.

  - ### createPattern

    public static Pattern createPattern(String input, boolean allowGlobbing, int options)
    Creates a regular expression Pattern that will match all strings that match exactly the given input string.

    This method can be used with `Matcher.matches()` or `Matcher.find()`.

    Parameters:
      `input` - the string that you want to your matched strings to exactly match.
      `allowGlobbing` - if true, globing characters (* and ?) will converted to regex wildcard patterns; otherwise, they will be escaped and searched as literals.
      `options` - any `Pattern` options desired. For example, you can pass `Pattern.CASE_INSENSITIVE` to get case insensitivity.
    Returns:
      a regular expression Pattern that will match all strings that exactly match with the given input string.

  - ### createPatternString

    public static String createPatternString(String input, boolean allowGlobbing)
    Creates a regular expression that can be used to create a Pattern that will match all strings that match the given input string.

    This method can be used with `Matcher.matches()` or `Matcher.find()`.

    Parameters:
      `input` - the string that you want to your matched strings to exactly match.
      `allowGlobbing` - if true, globing characters (* and ?) will converted to regex wildcard patterns; otherwise, they will be escaped and searched as literals.
    Returns:
      a regular expression Pattern String that will match all strings that exactly match with the given input string.