1 11 package org.eclipse.pde.internal.core.util; 12 13 import java.util.regex.Pattern ; 14 15 public class PatternConstructor { 16 private static final Pattern PATTERN_BACK_SLASH = Pattern.compile("\\\\"); 18 private static final Pattern PATTERN_QUESTION = Pattern.compile("\\?"); 20 private static final Pattern PATTERN_STAR = Pattern.compile("\\*"); 22 private static final Pattern PATTERN_LBRACKET = Pattern.compile("\\("); 24 private static final Pattern PATTERN_RBRACKET = Pattern.compile("\\)"); 29 private static String asRegEx(String pattern, boolean group) { 30 String result1 = PATTERN_BACK_SLASH.matcher(pattern).replaceAll( 33 "\\\\E\\\\\\\\\\\\Q"); String result2 = PATTERN_STAR.matcher(result1).replaceAll( 35 "\\\\E.*\\\\Q"); String result3 = PATTERN_QUESTION.matcher(result2).replaceAll( 37 "\\\\E.\\\\Q"); if (group) { 39 result3 = PATTERN_LBRACKET.matcher(result3).replaceAll( 40 "\\\\E(\\\\Q"); result3 = PATTERN_RBRACKET.matcher(result3).replaceAll( 42 "\\\\E)\\\\Q"); } 44 return "\\Q" + result3 + "\\E"; } 46 47 57 public static Pattern createPattern(String pattern, boolean isCaseSensitive) { 58 if (isCaseSensitive) 59 return Pattern.compile(asRegEx(pattern, false)); 60 return Pattern.compile(asRegEx(pattern, false), Pattern.CASE_INSENSITIVE 61 | Pattern.UNICODE_CASE); 62 } 63 public static Pattern createGroupedPattern(String pattern, boolean isCaseSensitive) { 64 if (isCaseSensitive) 65 return Pattern.compile(asRegEx(pattern, true)); 66 return Pattern.compile(asRegEx(pattern, true), Pattern.CASE_INSENSITIVE 67 | Pattern.UNICODE_CASE); 68 } 69 70 private PatternConstructor() { 71 } 72 } 73 | Popular Tags |