RegexQuery正则搜索

前端之家收集整理的这篇文章主要介绍了RegexQuery正则搜索前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/* * RegexQuery * 基于正则表达式的查询 * */ package query; import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; 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.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.regex.RegexQuery; public class RegexQueryTest { public RegexQueryTest(String INDEX_STORE_PATH){ try{ IndexWriter writer = new IndexWriter(INDEX_STORE_PATH,new StandardAnalyzer(),true); writer.setUseCompoundFile(false); Document doc1 = new Document(); Document doc2 = new Document(); Document doc3 = new Document(); Field f1 = new Field("url","http://www.abc.com/product?typeid=1&category=10&item=71",Field.Store.YES,Field.Index.TOKENIZED); Field f2 = new Field("url","http://www.def.com/product/show?typeid=3&catgory=10&item=58",Field.Index.TOKENIZED); Field f3 = new Field("url","http://www.ghi.com/prodduct/list?category=4&type=10&order=32",Field.Index.TOKENIZED); doc1.add(f1); doc2.add(f2); doc3.add(f3); writer.addDocument(doc1); writer.addDocument(doc2); writer.addDocument(doc3); writer.close(); IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH); //创建一个正则表达式,用于屁【诶所有的域名为abc.com的地址 String regex = "http://[a-z]{1,3}\\.abc\\.com/.*"; //构建一个Term Term t = new Term("url",regex); //创建正则查询 RegexQuery query = new RegexQuery(t); Hits hits = searcher.search(query); for(int i = 0; i < hits.length(); i++){ System.out.println(hits.doc(i)); } }catch(IOException e){ System.out.println("查找有误,请仔细检查"); e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub RegexQueryTest qq = new RegexQueryTest("E:\\Lucene项目\\索引文件"); } } 原文链接:https://www.f2er.com/regex/360611.html

猜你在找的正则表达式相关文章