public static boolean textMatchesWithANY ( String haystack , String needle ) { haystack = haystack . trim ( ) ; needle = needle . trim ( ) ; // Make sure we escape every character that is considered to be a special character inside a // regular expression. Matcher.quoteReplacement() takes care of '\\' and '$'. needle = Matcher . quoteReplacement ( needle ) ; String chars_to_be_escaped = ".|*?+(){}[]^" ; for ( char c : chars_to_be_escaped . toCharArray ( ) ) { String regex = "\\" + c ; String replacement = "\\\\" + c ; needle = needle . replaceAll ( regex , replacement ) ; } String pattern = needle . replaceAll ( ANY_MATCHER , "(?:.+)" ) ; logger . finest ( "Looking for pattern '" + pattern + "' in '" + haystack + "'" ) ; return haystack . matches ( pattern ) ; }
Compares haystack and needle taking into the account that the needle may contain any number of ANY_MATCHER occurrences that will be matched to any substring in haystack i . e . Show _ANY_ more ... will match anything like Show 1 more ... Show 2 more ... and so on .