title: RegEx matching using Lucene
body: <p>I would like to find "Bug reports" with Lucene using a regular expression, but whenever I try it doesn't work.</p>
  
  <p>I used the code from the <a href="http://svn.apache.org/viewvc/lucene/java/tags/lucene_3_0_3/contrib/regex/src/test/org/apache/lucene/search/regex/TestRegexQuery.java?revision=1040994&amp;view=co" rel="nofollow">Lucene page</a> to avoid a bad setup.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.regex.Pattern;
  
  import org.apache.lucene.analysis.SimpleAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.index.Term;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.regex.JakartaRegexpCapabilities;
  import org.apache.lucene.search.regex.RegexCapabilities;
  import org.apache.lucene.search.regex.RegexQuery;
  import org.apache.lucene.store.RAMDirectory;
  
  public class Rege {
  
    private static IndexSearcher searcher;
    private static final String FN = "field";
  
    public static void main(String[] args) throws Exception {
      RAMDirectory directory = new RAMDirectory();
      try {
  
        IndexWriter writer = new IndexWriter(directory,
            new SimpleAnalyzer(), true,
            IndexWriter.MaxFieldLength.LIMITED);
        Document doc = new Document();
        doc
            .add(new Field(
                FN,
                "[Phpmyadmin-devel] Commits against bug 601721 (Cookie auth mode faulty with IIS)",
                Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc);
        writer.optimize();
        writer.close();
        searcher = new IndexSearcher(directory, true);
  
      } catch (Exception e) {
        e.printStackTrace();
      }
  
      System.err.println(regexQueryNrHits("bug [0-9]+",null));
  
    }
  
    private static Term newTerm(String value) {
      return new Term(FN, value);
    }
  
    private static int regexQueryNrHits(String regex,
        RegexCapabilities capability) throws Exception {
  
      RegexQuery query = new RegexQuery(newTerm(regex));
  
      if (capability != null)
        query.setRegexImplementation(capability);
  
      return searcher.search(query, null, 1000).totalHits;
    }
  
  }
  </code></pre>
  
  <p>I would expect <code>bug [0-9]+</code> to return <code>1</code> but it doesn't. I also tested the regex with Java and it worked.</p>