Aspose.Words for Java でテキストを検索および置換する
Aspose.Words for Java でのテキストの検索と置換の概要
Aspose.Words for Javaは、Word文書をプログラムで操作できる強力なJava APIです。Word文書を扱う際によく行われるタスクの一つは、テキストの検索と置換です。テンプレート内のプレースホルダーを更新する必要がある場合でも、より複雑なテキスト操作を実行する必要がある場合でも、Aspose.Words for Javaは効率的に目的を達成するのに役立ちます。
前提条件
テキストの検索と置換の詳細に入る前に、次の前提条件が満たされていることを確認してください。
- Java開発環境
- Aspose.Words for Java ライブラリ
- 作業に使えるサンプルのWord文書
Aspose.Words for Javaライブラリは以下からダウンロードできます。 ここ .
単純なテキストの検索と置換
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// ドキュメントビルダーを作成する
DocumentBuilder builder = new DocumentBuilder(doc);
// テキストの検索と置換
builder.getRange().replace("old-text", "new-text", new FindReplaceOptions());
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、Word文書を読み込み、 DocumentBuilder
、そして replace
文書内の「古いテキスト」を検索して「新しいテキスト」に置き換える方法。
正規表現の使用
正規表現は、テキスト検索と置換のための強力なパターンマッチング機能を提供します。Aspose.Words for Java は、より高度な検索と置換操作のための正規表現をサポートしています。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// ドキュメントビルダーを作成する
DocumentBuilder builder = new DocumentBuilder(doc);
// 正規表現を使用してテキストを検索および置換する
Pattern regex = Pattern.compile("your-pattern");
builder.getRange().replace(regex, "replacement-text", new FindReplaceOptions());
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、正規表現パターンを使用して、ドキュメント内のテキストを検索して置換します。
フィールド内のテキストを無視する
検索および置換操作を実行するときに、フィールド内のテキストを無視するように Aspose.Words を構成できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、IgnoreFieldsをtrueに設定する
FindReplaceOptions options = new FindReplaceOptions();
options.setIgnoreFields(true);
// テキストを置換するときにオプションを使用する
doc.getRange().replace("text-to-replace", "new-text", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これは、マージ フィールドなどのフィールド内のテキストを置換対象から除外する場合に便利です。
削除リビジョン内のテキストを無視
検索および置換操作中に削除リビジョン内のテキストを無視するように Aspose.Words を構成できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、IgnoreDeletedをtrueに設定する
FindReplaceOptions options = new FindReplaceOptions();
options.setIgnoreDeleted(true);
// テキストを置換するときにオプションを使用する
doc.getRange().replace("text-to-replace", "new-text", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、変更履歴で削除対象としてマークされたテキストを置換対象から除外することができます。
挿入リビジョン内のテキストを無視する
検索および置換操作中に挿入リビジョン内のテキストを無視するように Aspose.Words を構成できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、IgnoreInsertedをtrueに設定する
FindReplaceOptions options = new FindReplaceOptions();
options.setIgnoreInserted(true);
// テキストを置換するときにオプションを使用する
doc.getRange().replace("text-to-replace", "new-text", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、変更履歴に挿入としてマークされたテキストを置換対象から除外することができます。
テキストをHTMLに置き換える
Aspose.Words for Java を使用して、テキストを HTML コンテンツに置き換えることができます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// カスタム置換コールバックを使用して FindReplaceOptions インスタンスを作成する
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new ReplaceWithHtmlEvaluator(options));
// テキストを置換するときにオプションを使用する
doc.getRange().replace("text-to-replace", "new-html-content", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、カスタム ReplaceWithHtmlEvaluator
テキストを HTML コンテンツに置き換えます。
ヘッダーとフッターのテキストの置き換え
Word 文書のヘッダーとフッター内のテキストを検索して置換できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// ヘッダーとフッターのコレクションを取得する
HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters();
// テキストを置換するヘッダーまたはフッターの種類を選択します(例:HeaderFooterType.FOOTER_PRIMARY)
HeaderFooter footer = headersFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
// FindReplaceOptionsインスタンスを作成し、フッターの範囲に適用します。
FindReplaceOptions options = new FindReplaceOptions();
footer.getRange().replace("text-to-replace", "new-text", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、特にヘッダーとフッター内のテキスト置換を実行できます。
ヘッダーとフッターの順序の変更を表示する
Aspose.Words を使用すると、ドキュメント内のヘッダーとフッターの順序の変更を表示できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// 最初のセクションを取得する
Section firstPageSection = doc.getFirstSection();
// FindReplaceOptionsインスタンスを作成し、それをドキュメントの範囲に適用します。
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new ReplaceLog());
// ヘッダーとフッターの順序に影響するテキストを置き換える
doc.getRange().replace(Pattern.compile("(header|footer)"), "", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、ドキュメント内のヘッダーとフッターの順序に関連する変更を視覚化できます。
テキストをフィールドに置き換える
Aspose.Words for Java を使用してテキストをフィールドに置き換えることができます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、フィールドのカスタム置換コールバックを設定します。
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new ReplaceTextWithFieldHandler(FieldType.FIELD_MERGE_FIELD));
// テキストを置換するときにオプションを使用する
doc.getRange().replace(Pattern.compile("PlaceHolder(\\d+)"), "", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、テキストをフィールドに置き換え、フィールドタイプを指定します(例: FieldType.FIELD_MERGE_FIELD
)。
評価者との交代
カスタム評価ツールを使用すると、置換テキストを動的に決定できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、カスタム置換コールバックを設定する
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new MyReplaceEvaluator());
// テキストを置換するときにオプションを使用する
doc.getRange().replace(Pattern.compile("[s|m]ad"), "", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、カスタム評価器(MyReplaceEvaluator
)を使用してテキストを置き換えます。
正規表現による置換
Aspose.Words for Java を使用すると、正規表現を使用してテキストを置き換えることができます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// 正規表現を使用してテキストを検索および置換する
doc.getRange().replace(Pattern.compile("[s|m]ad"), "bad", new FindReplaceOptions());
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、正規表現パターンを使用して、ドキュメント内のテキストを検索して置換します。
置換パターン内の認識と置換
Aspose.Words for Java を使用すると、置換パターン内での置換を認識して実行できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// UseSubstitutionsをtrueに設定してFindReplaceOptionsインスタンスを作成します。
FindReplaceOptions options = new FindReplaceOptions();
options.setUseSubstitutions(true);
// テキストをパターンに置き換えるときにオプションを使用する
doc.getRange().replace(Pattern.compile("([A-z]+) give money to ([A-z]+)"), "$2 take money from $1", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、置換パターン内で置換を実行し、より高度な置換を行うことができます。
文字列で置き換える
Aspose.Words for Java を使用して、テキストを単純な文字列に置き換えることができます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// テキストを文字列に置き換える
doc.getRange().replace("text-to-replace", "new-string", new FindReplaceOptions());
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
この例では、ドキュメント内の「text-to-replace」を「new-string」に置き換えます。
レガシーオーダーの使用
検索と置換の操作を実行するときに、従来の順序を使用できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// FindReplaceOptionsインスタンスを作成し、UseLegacyOrderをtrueに設定する
FindReplaceOptions options = new FindReplaceOptions();
options.setUseLegacyOrder(true);
// テキストを置換するときにオプションを使用する
doc.getRange().replace(Pattern.compile("\\[(.*?)\\]"), "", options);
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、検索と置換の操作に従来の順序を使用できるようになります。
表内のテキストの置き換え
Word 文書内の表内のテキストを検索して置換できます。
// ドキュメントを読み込む
Document doc = new Document("your-document.docx");
// 特定のテーブル(例:最初のテーブル)を取得する
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// 表内のテキストを置換するには FindReplaceOptions を使用します
table.getRange().replace("old-text", "new-text", new FindReplaceOptions());
// 変更したドキュメントを保存する
doc.save("modified-document.docx");
これにより、テーブル内でのみテキストの置換を実行できるようになります。
結論
Aspose.Words for Javaは、Word文書内のテキスト検索と置換のための包括的な機能を提供します。単純なテキスト置換から、正規表現、フィールド操作、カスタム評価関数を使用した高度な操作まで、Aspose.Words for Javaはあらゆるニーズに対応します。Asposeが提供する豊富なドキュメントとサンプルコードを活用して、この強力なJavaライブラリの潜在能力を最大限に活用してください。
よくある質問
Aspose.Words for Java をダウンロードするにはどうすればいいですか?
Aspose.Words for Javaは、次のウェブサイトからダウンロードできます。 このリンク .
テキスト置換に正規表現を使用できますか?
はい、Aspose.Words for Javaでは、テキスト置換に正規表現を使用できます。これにより、より高度で柔軟な検索と置換操作を実行できます。
置換中にフィールド内のテキストを無視するにはどうすればよいですか?
置換時にフィールド内のテキストを無視するには、 IgnoreFields
の財産 FindReplaceOptions
に true
これにより、マージ フィールドなどのフィールド内のテキストが置換から除外されます。
ヘッダーとフッター内のテキストを置き換えることはできますか?
はい、Word文書のヘッダーとフッター内のテキストを置き換えることができます。適切なヘッダーまたはフッターにアクセスし、 replace
希望する方法 FindReplaceOptions
。
UseLegacyOrder オプションの目的は何ですか?
その UseLegacyOrder
オプション FindReplaceOptions
検索と置換操作を実行する際に、従来の順序を使用できます。これは、従来の順序の動作が求められる特定のシナリオで役立ちます。