Index: /trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java	(revision 2509)
+++ /trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java	(revision 2510)
@@ -31,5 +31,5 @@
     private boolean caseSensitive = false;
     private boolean regexSearch = false;
-    private String  rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
+    private static String  rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
     private PushbackTokenizer tokenizer;
     private CollectBackReferencesVisitor childBackRefs;
@@ -43,5 +43,5 @@
 
     abstract public static class Match {
-        abstract public boolean match(OsmPrimitive osm) throws ParseError;
+        abstract public boolean match(OsmPrimitive osm);
     }
 
@@ -55,5 +55,5 @@
         private final Match match;
         public Not(Match match) {this.match = match;}
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
+        @Override public boolean match(OsmPrimitive osm) {
             return !match.match(osm);
         }
@@ -65,5 +65,5 @@
         private Match rhs;
         public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
+        @Override public boolean match(OsmPrimitive osm) {
             return lhs.match(osm) && rhs.match(osm);
         }
@@ -75,5 +75,5 @@
         private Match rhs;
         public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
+        @Override public boolean match(OsmPrimitive osm) {
             return lhs.match(osm) || rhs.match(osm);
         }
@@ -90,11 +90,41 @@
     }
 
-    private class KeyValue extends Match {
-        private String key;
-        private String value;
-        public KeyValue(String key, String value) {this.key = key; this.value = value; }
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
-
+    private static class KeyValue extends Match {
+        private final String key;
+        private final Pattern keyPattern;
+        private final String value;
+        private final Pattern valuePattern;
+        private final boolean caseSensitive;
+
+        public KeyValue(String key, String value, boolean regexSearch, boolean caseSensitive) throws ParseError {
+            this.caseSensitive = caseSensitive;
             if (regexSearch) {
+                int searchFlags = regexFlags(caseSensitive);
+
+                try {
+                    this.keyPattern = Pattern.compile(key, searchFlags);
+                    this.valuePattern = Pattern.compile(value, searchFlags);
+                } catch (PatternSyntaxException e) {
+                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
+                }
+                this.key = key;
+                this.value = value;
+
+            } else if (caseSensitive) {
+                this.key = key;
+                this.value = value;
+                this.keyPattern = null;
+                this.valuePattern = null;
+            } else {
+                this.key = key.toLowerCase();
+                this.value = value;
+                this.keyPattern = null;
+                this.valuePattern = null;
+            }
+        }
+
+        @Override public boolean match(OsmPrimitive osm) {
+
+            if (keyPattern != null) {
                 if (!osm.hasKeys())
                     return false;
@@ -107,24 +137,13 @@
                  */
 
-                Pattern searchKey   = null;
-                Pattern searchValue = null;
-                int searchFlags = regexFlags();
-
-                try {
-                    searchKey = Pattern.compile(key, searchFlags);
-                    searchValue = Pattern.compile(value, searchFlags);
-                } catch (PatternSyntaxException e) {
-                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
-                }
-
                 for (Entry<String, String> e : osm.entrySet()) {
                     String k = e.getKey();
                     String v = e.getValue();
 
-                    Matcher matcherKey = searchKey.matcher(k);
+                    Matcher matcherKey = keyPattern.matcher(k);
                     boolean matchedKey = matcherKey.find();
 
                     if (matchedKey) {
-                        Matcher matcherValue = searchValue.matcher(v);
+                        Matcher matcherValue = valuePattern.matcher(v);
                         boolean matchedValue = matcherValue.find();
 
@@ -146,10 +165,9 @@
 
                 String v1 = caseSensitive ? value : value.toLowerCase();
-                String v2 = caseSensitive ? this.value : this.value.toLowerCase();
 
                 // is not Java 1.5
                 //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
                 //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
-                return v1.indexOf(v2) != -1;
+                return v1.indexOf(value) != -1;
             }
 
@@ -224,5 +242,5 @@
 
         @Override
-        public boolean match(OsmPrimitive osm) throws ParseError {
+        public boolean match(OsmPrimitive osm) {
 
             if (!osm.hasKeys())
@@ -279,31 +297,35 @@
     }
 
-    private class Any extends Match {
-        private String s;
-        public Any(String s) {this.s = s;}
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
-            if (!osm.hasKeys())
-                return s.equals("");
-
-            String search;
-            Pattern searchRegex = null;
-
+    private static class Any extends Match {
+        private final String search;
+        private final Pattern searchRegex;
+        private final boolean caseSensitive;
+
+        public Any(String s, boolean regexSearch, boolean caseSensitive) throws ParseError {
+            this.caseSensitive = caseSensitive;
             if (regexSearch) {
-                search = s;
-                int searchFlags = regexFlags();
-
                 try {
-                    searchRegex = Pattern.compile(search, searchFlags);
+                    this.searchRegex = Pattern.compile(s, regexFlags(caseSensitive));
                 } catch (PatternSyntaxException e) {
                     throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
                 }
-            } else {
-                search = caseSensitive ? s : s.toLowerCase();
-            }
+                this.search = s;
+            } else if (caseSensitive) {
+                this.search = s;
+                this.searchRegex = null;
+            } else {
+                this.search = s.toLowerCase();
+                this.searchRegex = null;
+            }
+        }
+
+        @Override public boolean match(OsmPrimitive osm) {
+            if (!osm.hasKeys())
+                return search.equals("");
 
             // is not Java 1.5
             //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC);
             for (Entry<String, String> e : osm.entrySet()) {
-                if (regexSearch) {
+                if (searchRegex != null) {
                     String key = e.getKey();
                     String value = e.getValue();
@@ -343,5 +365,7 @@
             return false;
         }
-        @Override public String toString() {return s;}
+        @Override public String toString() {
+            return search;
+        }
     }
 
@@ -475,5 +499,5 @@
         private Match child;
         public Parent(Match m) { child = m; }
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
+        @Override public boolean match(OsmPrimitive osm) {
             boolean isParent = false;
 
@@ -516,5 +540,5 @@
         }
 
-        @Override public boolean match(OsmPrimitive osm) throws ParseError {
+        @Override public boolean match(OsmPrimitive osm) {
             boolean isChild = false;
             childBackRefs.initialize();
@@ -631,5 +655,5 @@
             return new Parent(parseParens());
         else
-            return new Any(tok);
+            return new Any(tok, regexSearch, caseSensitive);
     }
 
@@ -639,5 +663,5 @@
         else if (key.equals("user"))
             return new UserMatch(value);
-        else if (key.equals("tags"))
+        else if (key.equals("tags")) {
             try {
                 String[] range = value.split("-");
@@ -651,5 +675,5 @@
                 throw new ParseError(tr("Incorrect value of tags operator: {0}. Tags operator expects number of tags or range, for example tags:1 or tags:2-5", value));
             }
-        else if (key.equals("nodes")) {
+        } else if (key.equals("nodes")) {
             try {
                 String[] range = value.split("-");
@@ -671,8 +695,8 @@
             }
         } else
-            return new KeyValue(key, value);
-    }
-
-    private int regexFlags() {
+            return new KeyValue(key, value, regexSearch, caseSensitive);
+    }
+
+    private static int regexFlags(boolean caseSensitive) {
         int searchFlags = 0;
 
