#

Regex Caching

Häufig benutzt: Reguläre Ausdrücke in Java.
Jedoch ist das Neuinstanziieren eines Patterns / Matchers zeitintensiv. Ein einfaches Caching auf Basis von ThreadLocal hilft hier, die nicht-threadsafen Matcher zu cachen:

public class RegexUtils {
  private static ThreadLocal<Map<String, Matcher>> CASESENSITIVE_MATCHER_CACHE = 
    new ThreadLocal<Map<String,Matcher>>(){
      protected Map<String, Matcher> initialValue(){
        return new HashMap<String, Matcher>();
      }
    };
 
  private static ThreadLocal<Map<String, Matcher>> CASEINSENSITIVE_MATCHER_CACHE = 
    new ThreadLocal<Map<String,Matcher>>(){
      protected Map<String, Matcher> initialValue(){
        return new HashMap<String, Matcher>();
      }
    };
 
  public static boolean containsIgnoreCase(String text, String regex){
    if (text==null || text.length()<1){
      return false;
    }
    Matcher m = CASEINSENSITIVE_MATCHER_CACHE.get().get(regex);
    if (m==null){
      m = Pattern.compile(regex, Pattern.CANON_EQ|
        Pattern.CASE_INSENSITIVE|Pattern.DOTALL|
        Pattern.MULTILINE).matcher(""); 
      CASEINSENSITIVE_MATCHER_CACHE.get().put(regex, m);
    }
    return m.reset(text).find();
  }
 
  public static boolean contains(String text, String regex){
    if (text==null || text.length()<1){
      return false;
    }
    Matcher m = CASESENSITIVE_MATCHER_CACHE.get().get(regex);
    if (m==null){
      m = Pattern.compile(regex, Pattern.CANON_EQ|
        Pattern.DOTALL|Pattern.MULTILINE).matcher("");
      CASESENSITIVE_MATCHER_CACHE.get().put(regex, m);
    }
    return m.reset(text).find();
  }
 
  public static final Matcher getCaseinsensitiveMatcher(String text, String regex){
    Matcher m = CASEINSENSITIVE_MATCHER_CACHE.get().get(regex);
    if (m==null){
      m = Pattern.compile(regex, Pattern.CANON_EQ|
        Pattern.CASE_INSENSITIVE|Pattern.DOTALL|
        Pattern.MULTILINE).matcher(""); 
      CASEINSENSITIVE_MATCHER_CACHE.get().put(regex, m);
    }
    return m.reset(text);
  }
 
  public static final Matcher getCasesensitiveMatcher(String text, String regex){
    Matcher m = CASESENSITIVE_MATCHER_CACHE.get().get(regex);
    if (m==null){
      m = Pattern.compile(regex, Pattern.CANON_EQ|
        Pattern.DOTALL|
        Pattern.MULTILINE).matcher(""); 
      CASESENSITIVE_MATCHER_CACHE.get().put(regex, m);
    }
    return m.reset(text);
  }
Tags:, , , , ,

Leave a Reply »»

Note: All comments are manually approved to avoid spam. So if your comment doesn't appear immediately, that's ok. Have patience, it can take some days until I have the time to approve my comments.