Package ghidra.util

Class UserSearchUtils

java.lang.Object
ghidra.util.UserSearchUtils

public class UserSearchUtils extends Object
This class converts user inputted strings and creates Patterns 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 Details

    • STAR

      public static final String STAR
      Wildcard string for matching 0 or more characters.
      See Also:
    • 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.
    • escapeNonGlobbingRegexCharacters

      public static String escapeNonGlobbingRegexCharacters(String input)
      Escapes all special regex characters except globbing chars (*?)
      Parameters:
      input - the string to sanitize
      Returns:
      a new string with all non-globing regex characters escaped.