Skip to content

Apache Lucene open-source search software

LUCENE-1683: fixed JavaUtilRegexCapabilities (used by RegexQuery) to match entire string not just prefix

Michael McCandlessbbcab117d985
diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txt
index 7d88fd6aec0..84be15381bb 100644
--- a/contrib/CHANGES.txt
+++ b/contrib/CHANGES.txt
@@ -47,6 +47,11 @@ Bug fixes
8. LUCENE-1491: EdgeNGramTokenFilter no longer stops on tokens shorter than minimum n-gram size.
(Todd Teak via Otis Gospodnetic)
+ 9. LUCENE-1683: Fixed JavaUtilRegexCapabilities (an impl used by
+ RegexQuery) to use Matcher.matches() not Matcher.lookingAt() so
+ that the regexp must match the entire string, not just a prefix.
+ (Trejkaz via Mike McCandless)
+
New features
1. LUCENE-1531: Added support for BoostingTermQuery to XML query parser. (Karl Wettin)
diff --git a/contrib/regex/src/java/org/apache/lucene/search/regex/JavaUtilRegexCapabilities.java b/contrib/regex/src/java/org/apache/lucene/search/regex/JavaUtilRegexCapabilities.java
index e950ab70b24..cad9725a26e 100644
--- a/contrib/regex/src/java/org/apache/lucene/search/regex/JavaUtilRegexCapabilities.java
+++ b/contrib/regex/src/java/org/apache/lucene/search/regex/JavaUtilRegexCapabilities.java
@@ -71,7 +71,7 @@ public class JavaUtilRegexCapabilities implements RegexCapabilities {
}
public boolean match(String string) {
- return pattern.matcher(string).lookingAt();
+ return pattern.matcher(string).matches();
}
public String prefix() {
diff --git a/contrib/regex/src/test/org/apache/lucene/search/regex/TestRegexQuery.java b/contrib/regex/src/test/org/apache/lucene/search/regex/TestRegexQuery.java
index 1947263f05e..38bddda906f 100644
--- a/contrib/regex/src/test/org/apache/lucene/search/regex/TestRegexQuery.java
+++ b/contrib/regex/src/test/org/apache/lucene/search/regex/TestRegexQuery.java
@@ -25,6 +25,7 @@ import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;
@@ -77,6 +78,13 @@ public class TestRegexQuery extends TestCase {
return searcher.search(query).length();
}
+ public void testMatchAll() throws Exception {
+ TermEnum terms = new RegexQuery(new Term(FN, "jum.")).getEnum(searcher.getIndexReader());
+ // no term should match
+ assertNull(terms.term());
+ assertFalse(terms.next());
+ }
+
public void testRegex1() throws Exception {
assertEquals(1, regexQueryNrHits("^q.[aeiou]c.*$", null));
}