1 23 24 29 30 package com.sun.appserv.management.util.misc; 31 32 import java.util.regex.Pattern ; 33 34 37 public final class RegexUtil 38 { 39 private 40 RegexUtil() 41 { 42 } 44 45 46 private final static char BACKSLASH = '\\'; 47 48 51 public static final String REGEX_SPECIALS = BACKSLASH + "[]^$?+{}()|-!"; 52 53 54 55 61 public static Pattern [] 62 exprsToPatterns( final String [] exprs ) 63 { 64 return( exprsToPatterns( exprs, 0 ) ); 65 } 66 67 74 public static Pattern [] 75 exprsToPatterns( final String [] exprs, int flags ) 76 { 77 final Pattern [] patterns = new Pattern [ exprs.length ]; 78 79 for( int i = 0; i < exprs.length; ++i ) 80 { 81 patterns[ i ] = Pattern.compile( wildcardToJavaRegex( exprs[ i ]), flags ); 82 } 83 return( patterns ); 84 } 85 86 92 public static String 93 wildcardToJavaRegex( String input ) 94 { 95 String converted = input; 96 97 if ( input != null ) 98 { 99 final int length = input.length(); 100 final StringBuffer buf = new StringBuffer (); 101 102 for( int i = 0; i < length; ++i ) 103 { 104 final char theChar = input.charAt( i ); 105 106 if ( theChar == '.' ) 107 { 108 buf.append( "[.]" ); 109 } 110 else if ( theChar == '*' ) 111 { 112 buf.append( ".*" ); 113 } 114 else if ( REGEX_SPECIALS.indexOf( theChar ) >= 0 ) 115 { 116 buf.append( "" + BACKSLASH + theChar ); 118 } 119 else 120 { 121 buf.append( theChar ); 122 } 123 } 124 125 converted = buf.toString(); 126 127 } 128 return( converted ); 129 } 130 } 131 132 | Popular Tags |