찾기 및 바꾸기
키보드와 마우스를 사용하여 문서 내에서 쉽게 탐색 할 수 있지만 스크롤 할 페이지가 많은 경우 긴 문서에서 특정 텍스트를 찾는 데 시간이 오래 걸립니다. 그것은 당신이 당신의 문서에서 사용한 특정 문자 또는 단어를 대체 할 때 더 많은 시간이 소요 될 것입니다. “찾기 및 바꾸기"기능을 사용하면 문서에서 문자 시퀀스를 찾아 다른 문자 시퀀스로 바꿀 수 있습니다.
Aspose.Words을 사용하면Microsoft Word과 같은 추가 응용 프로그램을 설치 및 사용하지 않고도 문서에서 특정 문자열 또는 정규식 패턴을 찾아 대체 문자열로 바꿀 수 있습니다. 이렇게하면 많은 타이핑 및 포맷 작업 속도가 빨라져 잠재적으로 작업 시간을 절약 할 수 있습니다.
이 문서에서는 메타 문자를 지원하는 문자열 대체 및 정규식을 적용하는 방법에 대해 설명합니다.
{#ways-to-find-and-replace}찾기 및 바꾸기 방법
Aspose.Words은 다음을 사용하여 찾기 및 바꾸기 작업을 적용하는 두 가지 방법을 제공합니다:
- Simple string replacement-특정 문자열을 찾아서 다른 문자열로 바꾸려면 모든 발생에 따라 다른 지정된 대체 문자열로 바꿀 검색 문자열(영숫자 문자)을 지정해야 합니다. 두 문자열 모두 기호를 포함해서는 안 됩니다. 문자열 비교는 대/소문자를 구분할 수 있거나 철자가 확실하지 않거나 여러 가지 유사한 철자가 있을 수 있습니다.
- Regular expressions-정규 표현식을 지정하여 정확한 문자열 일치를 찾아 정규 표현식에 따라 바꿉니다. 단어 는 알파뉴메릭 문자 만 으로 구성 되어 있다고 정의 되어 있다는 점 에 유의 하십시오. 만약 대체가 전체 단어만 일치하고 입력 문자열이 기호를 포함한다면,어떤 문장도 발견되지 않을 것입니다.
또한 간단한 문자열 대체 및 정규식이 포함된 특수 메타 문자를 사용하여 찾기 및 바꾸기 작업 내에서 나누기를 지정할 수 있습니다.
Aspose.Words는 찾기 및 바꾸기 기능을IReplacingCallBack으로 표시합니다. FindReplaceOptions클래스를 사용하여 찾기 및 바꾸기 프로세스 중에 많은 옵션을 사용할 수 있습니다.
간단한 문자열 대체 {#find-and-replace-text-using-simple-string-replacement}을 사용하여 텍스트 찾기 및 바꾸기
Replace메서드 중 하나를 사용하여 특정 문자열을 찾거나 바꾼 다음 만든 교체 수를 반환할 수 있습니다. 이 경우 대체할 문자열,모든 항목을 대체할 문자열,대/소문자를 구분하는지 여부 및 독립 실행형 단어만 영향을 받는지 여부를 지정할 수 있습니다.
다음 코드 예제에서는”CustomerName“문자열을 찾아*“James Bond”*문자열로 바꾸는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load a Word DOCX document by creating an instance of the Document class. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("Hello _CustomerName_,"); | |
// Specify the search string and replace string using the Replace method. | |
doc.getRange().replace("_CustomerName_", "James Bond", new FindReplaceOptions()); | |
// Save the result. | |
doc.save(dataDir + "Range.ReplaceSimple.docx"); |
간단한 문자열 교체를 적용하기 전에 문서 간의 차이점을 알 수 있습니다:

그리고 간단한 문자열 교체를 적용한 후:

{#find-and-replace-text-using-regular-expressions}정규식을 사용하여 텍스트 찾기 및 바꾸기
정규식(정규식)은 특정 텍스트 시퀀스를 설명하는 패턴입니다. 단어의 모든 이중 발생을 단일 단어 발생으로 바꾸려고 한다고 가정합니다. 그런 다음 다음 정규식을 적용하여 이중 단어 패턴을 지정할 수 있습니다.([a-zA-Z]+) \1
.
다른Replace메서드를 사용하여Regex
매개 변수를 정규식 패턴으로 설정하여 특정 문자 조합을 검색하고 대체하여 일치 항목을 찾습니다.
다음 코드 예제에서는 정규식 패턴과 일치하는 문자열을 지정된 대체 문자열로 바꾸는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("sad mad bad"); | |
if(doc.getText().trim() == "sad mad bad") | |
{ | |
System.out.println("Strings are equal!"); | |
} | |
// Replaces all occurrences of the words "sad" or "mad" to "bad". | |
FindReplaceOptions options = new FindReplaceOptions(); | |
doc.getRange().replace(Pattern.compile("[s|m]ad"), "bad", options); | |
// Save the Word document. | |
doc.save(dataDir + "Range.ReplaceWithRegex.docx"); |
문자열 대체를 정규식으로 적용하기 전에 문서 간의 차이점을 확인할 수 있습니다:

그리고 정규 표현식으로 문자열 대체를 적용한 후:

메타 문자 {#find-and-replace-text-using-metacharacters}를 사용하여 문자열 찾기 및 바꾸기
특정 텍스트 또는 구가 여러 단락,섹션 또는 페이지로 구성된 경우 검색 문자열 또는 대체 문자열에 메타 문자를 사용할 수 있습니다. 일부 메타문자에는 단락 나누기용**&p**,섹션 나누기용**&b**,페이지 나누기용**&m**및 줄 나누기용**&l**이 포함됩니다.
다음 코드 예제에서는 텍스트를 단락 및 페이지 나누기로 바꾸는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.getFont().setName("Arial"); | |
builder.writeln("First section"); | |
builder.writeln(" 1st paragraph"); | |
builder.writeln(" 2nd paragraph"); | |
builder.writeln("{insert-section}"); | |
builder.writeln("Second section"); | |
builder.writeln(" 1st paragraph"); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.getApplyParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
// Double each paragraph break after word "section", add kind of underline and make it centered. | |
int count = doc.getRange().replace("section&p", "section&p----------------------&p", options); | |
// Insert section break instead of custom text tag. | |
count = doc.getRange().replace("{insert-section}", "&b", options); | |
doc.save(dataDir + "ReplaceTextContaingMetaCharacters_out.docx"); |
{#find-and-replace-string-in-header-or-footer-of-a-document}문서의 머리글/바닥글에서 문자열 찾기 및 바꾸기
HeaderFooter클래스를 사용하여 단어 문서의 머리글/바닥글 섹션에서 텍스트를 찾아 바꿀 수 있습니다.
다음 코드 예제에서는 문서에서 머리글 섹션의 텍스트를 대체하는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the template document, containing obsolete copyright information in the footer. | |
Document doc = new Document(dataDir + "HeaderFooter.ReplaceText.doc"); | |
// Access header of the Word document. | |
HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); | |
HeaderFooter header = headersFooters.get(HeaderFooterType.HEADER_PRIMARY); | |
// Set options. | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.setMatchCase(false); | |
options.setFindWholeWordsOnly(false); | |
// Replace text in the header of the Word document. | |
header.getRange().replace("Aspose.Words", "Remove", options); | |
// Save the Word document. | |
doc.save(dataDir + "HeaderReplace.docx"); |
헤더 문자열 대체를 적용하기 전에 문서 간의 차이점을 확인할 수 있습니다:

그리고 헤더 문자열 교체를 적용한 후:

문서에서 바닥글 섹션의 텍스트를 대체하는 코드 예제는 이전 머리글 코드 예제와 매우 유사합니다. 다음 두 줄을 바꾸기 만하면됩니다:
HeaderFooter header = headersFooters.get(HeaderFooterType.HEADER_PRIMARY);
header.getRange().replace("Aspose.Words", "Remove", options);
다음 과 함께:
바닥글 문자열 대체를 적용하기 전에 문서의 차이점을 확인할 수 있습니다:

그리고 바닥 글 문자열 교체를 적용한 후:

{#ignore-text-during-find-and-replace}찾기 및 바꾸기 중 텍스트 무시
찾기 및 바꾸기 작업을 적용하는 동안 텍스트의 특정 세그먼트를 무시할 수 있습니다. 따라서 텍스트의 특정 부분을 검색에서 제외 할 수 있으며 찾기 및 바꾸기는 나머지 부분에만 적용 할 수 있습니다.
Aspose.Words는 다음과 같은 텍스트를 무시하기 위한 많은 찾기 및 바꾸기 속성을 제공합니다IgnoreDeleted, IgnoreFieldCodes, IgnoreFields, IgnoreFootnotes, 그리고IgnoreInserted
다음 코드 예제에서는 수정본 삭제 내에서 텍스트를 무시하는 방법을 보여 줍니다:
// Create new document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert non-revised text. | |
builder.writeln("Deleted"); | |
builder.write("Text"); | |
// Remove first paragraph with tracking revisions. | |
doc.startTrackRevisions("author", new Date()); | |
doc.getFirstSection().getBody().getFirstParagraph().remove(); | |
doc.stopTrackRevisions(); | |
Pattern regex = Pattern.compile("e", Pattern.CASE_INSENSITIVE); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
// Replace 'e' in document ignoring deleted text. | |
options.setIgnoreDeleted(true); | |
doc.getRange().replace(regex, "*", options); | |
System.out.println(doc.getText()); // The output is: Deleted\rT*xt\f | |
// Replace 'e' in document NOT ignoring deleted text. | |
options.setIgnoreDeleted(false); | |
doc.getRange().replace(regex, "*", options); | |
System.out.println(doc.getText()); // The output is: D*l*t*d\rT*xt\f |
찾기 및 바꾸기 작업 {#customize-find-and-replace-operation}사용자 지정
Aspose.Words는ApplyFont및ApplyParagraphFormats속성으로 특정 형식을 적용하고UseSubstitutions속성으로 대체 패턴의 대체를 사용하는 등 텍스트를 찾고 바꾸기 위해 다양한properties을 제공합니다.
다음 코드 예제에서는 문서에서 특정 단어를 강조 표시하는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Highlight word "the" with yellow color. | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.getApplyFont().setHighlightColor(Color.YELLOW); | |
// Replace highlighted text. | |
doc.getRange().replace("the", "the", options); |
Aspose.WordsIReplacingCallback인터페이스를 사용하여 바꾸기 작업 중에 사용자 지정 메서드를 만들고 호출할 수 있습니다. HTML태그로 정규 표현식으로 지정된 텍스트를 바꾸는 것과 같은 찾기 및 바꾸기 작업을 사용자 정의해야하는 몇 가지 사용 사례가있을 수 있으므로 기본적으로HTML삽입으로 바꾸기를 적용합니다.
문자열을HTML태그로 바꾸어야 하는 경우IReplacingCallback인터페이스를 적용하여 찾기 및 바꾸기 작업을 사용자 지정하여 문서의 일치 노드와 실행 시작 시 일치가 시작됩니다. IReplacingCallback을 사용하는 몇 가지 예를 들어 보겠습니다.
다음 코드 예제에서는 지정된 텍스트를HTML로 바꾸는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void ReplaceWithHtml() throws Exception { | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("Hello <CustomerName>,"); | |
FindReplaceOptions options = new FindReplaceOptions(); | |
options.setReplacingCallback(new ReplaceWithHtmlEvaluator()); | |
doc.getRange().replace(Pattern.compile(" <CustomerName>,"), "", options); | |
//doc.getRange().replace(" <CustomerName>,", html, options); | |
// Save the modified document. | |
doc.save(dataDir + "Range.ReplaceWithInsertHtml.doc"); | |
System.out.println("\nText replaced with meta characters successfully.\nFile saved at " + dataDir); | |
} | |
static class ReplaceWithHtmlEvaluator implements IReplacingCallback { | |
public int replacing(ReplacingArgs e) throws Exception { | |
// This is a Run node that contains either the beginning or the complete match. | |
Node currentNode = e.getMatchNode(); | |
// create Document Buidler and insert MergeField | |
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument()); | |
builder.moveTo(currentNode); | |
// Replace '<CustomerName>' text with a red bold name. | |
builder.insertHtml("<b><font color='red'>James Bond, </font></b>");e.getReplacement(); | |
currentNode.remove(); | |
//Signal to the replace engine to do nothing because we have already done all what we wanted. | |
return ReplaceAction.SKIP; | |
} | |
} |
다음 코드 예제에서는 녹색으로 양수를 강조 표시하고 빨간색으로 음수를 강조 표시하는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Replace and Highlight Numbers. | |
static class NumberHighlightCallback implements IReplacingCallback { | |
public int replacing (ReplacingArgs args) throws Exception { | |
Node currentNode = args.getMatchNode(); | |
// Let replacement to be the same text. | |
args.setReplacement(currentNode.getText()); | |
int val = currentNode.hashCode(); | |
// Apply either red or green color depending on the number value sign. | |
FindReplaceOptions options = new FindReplaceOptions(); | |
if(val > 0) | |
{ | |
options.getApplyFont().setColor(Color.GREEN); | |
} | |
else | |
{ | |
options.getApplyFont().setColor(Color.RED); | |
} | |
return ReplaceAction.REPLACE; | |
} | |
} |
다음 코드 예제에서는 각 줄에 줄 번호를 앞에 붙이는 방법을 보여 줍니다:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void TestLineCounter() throws Exception { | |
// Create a document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add lines of text. | |
builder.writeln("This is first line"); | |
builder.writeln("Second line"); | |
builder.writeln("And last line"); | |
// Prepend each line with line number. | |
FindReplaceOptions opt = new FindReplaceOptions(); | |
opt.setReplacingCallback(new LineCounterCallback()); | |
doc.getRange().replace(Pattern.compile("[^&p]*&p"), "", opt); | |
doc.save(dataDir + "TestLineCounter.docx"); | |
} | |
static class LineCounterCallback implements IReplacingCallback | |
{ | |
private int mCounter = 1; | |
public int replacing(ReplacingArgs args) throws Exception { | |
Node currentNode = args.getMatchNode(); | |
System.out.println(currentNode.getText()); | |
args.setReplacement(mCounter++ +"."+ currentNode.getText()); | |
return ReplaceAction.REPLACE; | |
} | |
} |