Package ghidra.util

Class UserSearchUtils


  • public class UserSearchUtils
    extends java.lang.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 Summary

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

      Constructors 
      Constructor Description
      UserSearchUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.regex.Pattern createContainsPattern​(java.lang.String input, boolean allowGlobbing, int options)
      Creates a regular expression Pattern that will match all strings that contain the given input string.
      static java.util.regex.Pattern createEndsWithPattern​(java.lang.String input, boolean allowGlobbing, int options)
      Creates a regular expression Pattern that will match all strings that end with the given input string.
      static java.util.regex.Pattern createLiteralSearchPattern​(java.lang.String text)
      Generate a compiled representation of a regular expression, ignoring regex special characters .
      static java.util.regex.Pattern createPattern​(java.lang.String input, boolean allowGlobbing, int options)
      Creates a regular expression Pattern that will match all strings that match exactly the given input string.
      static java.lang.String createPatternString​(java.lang.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 java.util.regex.Pattern createSearchPattern​(java.lang.String input, boolean caseSensitive)
      Note: this is the default model of how to let users search for things in Ghidra.
      static java.util.regex.Pattern createStartsWithPattern​(java.lang.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 Detail

      • STAR

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

        public static final java.util.regex.Pattern NON_GLOB_BACKSLASH_PATTERN
        A pattern that will find all '\' chars that are not followed by '*', '?' or another '\'
    • Constructor Detail

      • UserSearchUtils

        public UserSearchUtils()
    • Method Detail

      • createSearchPattern

        public static java.util.regex.Pattern createSearchPattern​(java.lang.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:
        java.util.regex.PatternSyntaxException - if the input could be compiled
      • createLiteralSearchPattern

        public static java.util.regex.Pattern createLiteralSearchPattern​(java.lang.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:
        java.util.regex.PatternSyntaxException - if the input could be compiled
      • createStartsWithPattern

        public static java.util.regex.Pattern createStartsWithPattern​(java.lang.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 java.util.regex.Pattern createEndsWithPattern​(java.lang.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 java.util.regex.Pattern createContainsPattern​(java.lang.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 java.util.regex.Pattern createPattern​(java.lang.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 java.lang.String createPatternString​(java.lang.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.