JavaでGroupDocs.Searchを使用した正規表現検索の方法
数千ものテキストドキュメントを検索することは、干し草の山から針を探すように感じられます。Javaで正規表現検索は、言語の強力な正規表現エンジンと、インデックスを構築して超高速パターンマッチングを実現するGroupDocs.Searchを組み合わせることで、簡単になります。数分でライブラリのインストール方法、インデックスの作成、ファイルの追加、シンプルなテキストベースとオブジェクト指向の正規表現クエリの実行方法を見ていきます。最後には、任意のJavaアプリケーションに堅牢なパターンマッチ検索を組み込む準備が整います。
クイック回答
- 主要なライブラリは何ですか? GroupDocs.Search for Java
- どうやって始めますか? Add the Maven dependency and instantiate an
Indexobject - 正規表現でコンテンツをフィルタリングできますか? Yes – use regex queries for content‑filtering scenarios
- ライセンスは必要ですか? A free trial or temporary license is required for production use
- サポートされているJDKバージョンはどれですか? Java 8 or higher
正規表現検索とは?
正規表現検索を使用すると、日付、メールアドレス、繰り返し文字などのパターンを多数のファイルから単一の操作で特定できます。プレーンテキストのクエリを強力なルールベースのスキャナに変換し、コンテンツをリアルタイムで抽出またはブロックできます。
正規表現検索にGroupDocs.Searchを使用する理由
GroupDocs.Searchはドキュメントを一度インデックス化し、そのインデックスをすべてのクエリで再利用するため、最大10倍速い検索を実現します。ライブラリは30以上のファイル形式(PDF、DOCX、XLSX、PPTX、TXT、HTML など)をサポートし、メモリに全ファイルをロードせずに数百ページのファイルも処理できます。
前提条件
- Java Development Kit (JDK) 8 or higher
- Maven for dependency management
- Basic familiarity with Java regular expressions
必要なライブラリと依存関係
Add GroupDocs.Search to your Maven project:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/search/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-search</artifactId>
<version>25.4</version>
</dependency>
</dependencies>
Alternatively, download the latest JAR from GroupDocs.Search for Java releases.
ライセンス取得
Obtain a free trial or temporary license from GroupDocs.License and load it at application start‑up.
Java向けGroupDocs.Searchの設定
インストール情報
- Maven Integration: Add the repository and dependency shown above to your
pom.xml. - Direct Download: Place the JAR files on your project’s classpath.
- License Application: Load the license file at application start‑up.
import com.groupdocs.search.*;
public class SearchSetup {
public static void main(String[] args) {
// Initialize the index by specifying a directory.
String indexFolder = "YOUR_DOCUMENT_DIRECTORY\\output\\AdvancedUsage\\Searching\\RegularExpressionSearch";
Index index = new Index(indexFolder);
System.out.println("Index created successfully at: " + indexFolder);
}
}
コアコンポーネント
The Index class is the core component that stores searchable tokens extracted from your documents. It enables rapid lookup of any term or pattern without re‑reading the original files.
インデックスの作成方法
Creating an index is straightforward: instantiate the Index class with a folder path where the index files will be stored. The constructor creates the necessary database files on first use and prepares the engine for adding and searching documents. Once created, reuse the same index for all queries.
String indexFolder = "YOUR_DOCUMENT_DIRECTORY\\output\\AdvancedUsage\\Searching\\RegularExpressionSearch";
Index index = new Index(indexFolder);
ドキュメントの追加方法
To make a file searchable, call index.add with a Document (or DocumentInfo) instance pointing to the file path. The library parses the content, extracts tokens, and stores them in the index. This operation can be performed for single files or batches, and updates are merged incrementally.
index.add("YOUR_DOCUMENT_DIRECTORY");
system.out.println("Documents added to the index.");
テキスト形式で正規表現検索を実行する方法
RegexQuery defines a regular‑expression based search query. Load a RegexQuery with a plain‑text pattern and pass it to the search method of the Index. The engine evaluates the pattern against the indexed tokens and returns matching document references, making one‑off lookups fast and simple.
String query1 = "^((.)\\2{1,})";
オブジェクト形式で正規表現検索を実行する方法
RegexQuery can also be built as an object and reused across multiple searches. Define the query once, configure options such as case‑insensitivity or fuzzy matching, and invoke index.search repeatedly. This approach improves performance when the same pattern is applied to many different document sets.
SearchResult result1 = index.search(query1);
system.out.println("Number of occurrences found: " + result1.getDocumentCount());
コンテンツフィルタリング正規表現のユースケース
You can employ regex to automatically block or flag content that matches certain patterns, such as:
- Detecting repeated characters for spam filtering
- Finding credit‑card‑like sequences for data‑privacy checks
- Extracting dates or IDs for downstream processing
実用的な応用例
- Document Management Systems: Locate contracts, invoices, or policies by pattern (e.g., invoice numbers).
- Content Moderation: Apply regex rules to moderate user‑generated text in forums or chat apps.
- Data Extraction: Pull structured data like order numbers from unstructured PDFs or Word files.
パフォーマンス上の考慮点
- Index Updates: Call
index.addwhenever source files change to keep results fresh. - Memory Management: For corpora exceeding 1 million documents, enable incremental indexing to keep heap usage under control.
- Regex Design: Keep patterns concise; a pattern like
\d{4}-\d{2}-\d{2}runs 3× faster than a wildcard‑heavy expression such as.*.
結論
You now know how to regex search in Java using GroupDocs.Search, from installing the library and creating an index to executing both text‑based and object‑oriented queries. These techniques let you add fast, pattern‑aware search to any Java application, whether you’re building a document portal, a compliance scanner, or a data‑mining pipeline.
よくある質問
Q: What is the difference between text‑based and object‑based regex queries in GroupDocs.Search?
A: Text‑based queries are quick one‑liners, while object‑based queries provide reusable, type‑safe definitions that can be stored and reused across multiple searches.
Q: Can GroupDocs.Search index non‑text documents such as PDFs or Excel files?
A: Yes, the library extracts searchable text from PDFs, DOCX, XLSX, PPTX, and over 30 other formats.
Q: How do I update an existing search index after adding new files?
A: Call index.add with the new or modified documents; the library will merge changes without rebuilding the whole index.
Q: What are common pitfalls when using regex with GroupDocs.Search?
A: Overly broad patterns (e.g., .*) can cause performance degradation, and malformed expressions may return no results. Always test patterns on a sample set first.
Q: Where can I find more advanced GroupDocs.Search tutorials?
A: Visit the GroupDocs Documentation for deep‑dive guides, API references, and sample projects.
最終更新日: 2026-07-31
テスト環境: GroupDocs.Search 25.4
作者: GroupDocs
SearchQuery query2 = SearchQuery.createRegexQuery("^(.)\\1{1,}");
SearchResult result2 = index.search(query2);
system.out.println("Occurrences found using object form: " + result2.getDocumentCount());