public static String toRegularExpression ( String likeExpression ) { // Replace all '\x' with 'x' ... String result = likeExpression . replaceAll ( "\\\\(.)" , "$1" ) ; // Escape characters used as metacharacters in regular expressions, including // '[', '^', '\', '$', '.', '|', '+', '(', and ')' // But leave '?' and '*' result = result . replaceAll ( "([$.|+()\\[\\\\^\\\\\\\\])" , "\\\\$1" ) ; // Replace '%'->'[.]*' and '_'->'[.] // (order of these calls is important!) result = result . replace ( "*" , ".*" ) . replace ( "?" , "." ) ; result = result . replace ( "%" , ".*" ) . replace ( "_" , "." ) ; // Replace all wildcards between square bracket literals with digit wildcards ... result = result . replace ( "\\[.*]" , "\\[\\d+]" ) ; return result ; }
Convert the JCR like expression to a regular expression . The JCR like expression uses % to match 0 or more characters _ to match any single character \ x to match the x character and all other characters to match themselves . Note that if any regex metacharacters appear in the like expression they will be escaped within the resulting regular expression .