public static String convertImageNamePattern ( String pattern ) { final String REGEX_PREFIX = "%regex[" , ANT_PREFIX = "%ant[" , PATTERN_SUFFIX = "]" ; if ( pattern . startsWith ( REGEX_PREFIX ) && pattern . endsWith ( PATTERN_SUFFIX ) ) { return pattern . substring ( REGEX_PREFIX . length ( ) , pattern . length ( ) - PATTERN_SUFFIX . length ( ) ) ; } if ( pattern . startsWith ( ANT_PREFIX ) && pattern . endsWith ( PATTERN_SUFFIX ) ) { pattern = pattern . substring ( ANT_PREFIX . length ( ) , pattern . length ( ) - PATTERN_SUFFIX . length ( ) ) ; } String [ ] parts = pattern . split ( "((?=[/:?*])|(?<=[/:?*]))" ) ; Matcher matcher = Pattern . compile ( "[A-Za-z0-9-]+" ) . matcher ( "" ) ; StringBuilder builder = new StringBuilder ( "^" ) ; for ( int i = 0 ; i < parts . length ; ++ i ) { if ( "?" . equals ( parts [ i ] ) ) { builder . append ( "[^/:]" ) ; } else if ( "*" . equals ( parts [ i ] ) ) { if ( i + 1 < parts . length && "*" . equals ( parts [ i + 1 ] ) ) { builder . append ( "([^:]|:(?=.*:))*" ) ; ++ i ; if ( i + 1 < parts . length && "/" . equals ( parts [ i + 1 ] ) ) { builder . append ( "(?<![^/])" ) ; ++ i ; } } else { builder . append ( "([^/:]|:(?=.*:))*" ) ; } } else if ( "/" . equals ( parts [ i ] ) || ":" . equals ( parts [ i ] ) || matcher . reset ( parts [ i ] ) . matches ( ) ) { builder . append ( parts [ i ] ) ; } else if ( parts [ i ] . length ( ) > 0 ) { builder . append ( Pattern . quote ( parts [ i ] ) ) ; } } builder . append ( "$" ) ; return builder . toString ( ) ; }
Accepts an Ant - ish or regular expression pattern and compiles to a regular expression .