찾기 및 바꾸기

키보드와 마우스를 사용하여 문서 내에서 쉽게 탐색 할 수 있지만 스크롤 할 페이지가 많은 경우 긴 문서에서 특정 텍스트를 찾는 데 시간이 오래 걸립니다. 그것은 당신이 당신의 문서에서 사용한 특정 문자 또는 단어를 대체 할 때 더 많은 시간이 소요 될 것입니다. “찾기 및 바꾸기"기능을 사용하면 문서에서 문자 시퀀스를 찾아 다른 문자 시퀀스로 바꿀 수 있습니다.

Aspose.Words 다음과 같은 추가 응용 프로그램을 설치 및 사용하지 않고도 문서에서 특정 문자열 또는 정규식 패턴을 찾아 대체 문자열로 바꿀 수 있습니다 Microsoft Word. 이렇게하면 많은 타이핑 및 포맷 작업 속도가 빨라져 잠재적으로 작업 시간을 절약 할 수 있습니다.

이 문서에서는 메타 문자를 지원하는 문자열 대체 및 정규식을 적용하는 방법에 대해 설명합니다.

찾기 및 바꾸기 방법

Aspose.Words 다음을 사용하여 찾기 및 바꾸기 작업을 적용하는 두 가지 방법을 제공합니다:

  1. Simple string replacement -찾아 다른 특정 문자열을 대체하려면,당신은 다른 지정된 대체 문자열로 모든 발생에 따라 대체 될 것입니다 검색 문자열(영숫자 문자)를 지정해야합니다. 두 문자열 모두 기호를 포함해서는 안 됩니다. 문자열 비교는 대/소문자를 구분할 수 있거나 철자가 확실하지 않거나 여러 가지 유사한 철자가 있을 수 있습니다.
  2. Regular expressions -정확한 문자열 일치를 찾아 정규 표현식에 따라 교체 정규 표현식을 지정합니다. 단어 는 알파뉴메릭 문자 만 으로 구성 되어 있다고 정의 되어 있다는 점 에 유의 하십시오. 만약 대체가 전체 단어만 일치하고 입력 문자열이 기호를 포함한다면,어떤 문장도 발견되지 않을 것입니다.

또한 간단한 문자열 대체 및 정규식이 포함된 특수 메타 문자를 사용하여 찾기 및 바꾸기 작업 내에서 나누기를 지정할 수 있습니다.

Aspose.Words 찾기 및 바꾸기 기능을 다음과 같이 표시합니다. Aspose.Words.Replacing 네임스페이스 다음을 사용하여 찾기 및 바꾸기 프로세스 중에 많은 옵션을 사용할 수 있습니다 FindReplaceOptions 수업

간단한 문자열 교체를 사용하여 텍스트 찾기 및 바꾸기

당신은 중 하나를 사용할 수 있습니다 Replace 메서드는 특정 문자열을 찾거나 대체 하 고 만든 대체의 수를 반환 합니다. 이 경우 대체할 문자열,모든 항목을 대체할 문자열,대/소문자를 구분하는지 여부 및 독립 실행형 단어만 영향을 받는지 여부를 지정할 수 있습니다.

다음 코드 예제에서는 문자열을 찾는 방법을 보여 줍니다."CustomerName“그리고 문자열로 대체 “James Bond”:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Load a Word Docx document by creating an instance of the Document class.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello _CustomerName_, ");
// Specify the search string and replace string using the Replace method.
doc->get_Range()->Replace(u"_CustomerName_", u"James Bond", System::MakeObject<FindReplaceOptions>());
// Save the result.
System::String outputPath = outputDataDir + u"Range.ReplaceSimple.docx";
doc->Save(outputPath);

간단한 문자열 교체를 적용하기 전에 문서 간의 차이점을 알 수 있습니다:

before-simple-string-replacement-aspose-words-cpp

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

after-simple-string-replacement-aspose-words-cpp

정규식을 사용하여 텍스트 찾기 및 바꾸기

정규식(정규식)은 특정 텍스트 시퀀스를 설명하는 패턴입니다. 단어의 모든 이중 발생을 단일 단어 발생으로 바꾸려고 한다고 가정합니다. 그런 다음 다음 정규식을 적용하여 이중 단어 패턴을 지정할 수 있습니다: ([a-zA-Z]+) \1.

다른 사용 Replace 검색 및 설정하여 특정 문자 조합을 대체하는 방법 Regex 일치 항목을 찾을 수있는 정규식 패턴으로 매개 변수.

다음 코드 예제에서는 정규식 패턴과 일치하는 문자열을 지정된 대체 문자열로 바꾸는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"sad mad bad");
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Replaces all occurrences of the words "sad" or "mad" to "bad".
doc->get_Range()->Replace(System::MakeObject<System::Text::RegularExpressions::Regex>(u"[s|m]ad"), u"bad", options);
const System::String outputPath = outputDataDir + u"FindAndReplaceWithRegex_out.doc";
doc->Save(outputPath);

문자열 대체를 정규식으로 적용하기 전에 문서 간의 차이점을 확인할 수 있습니다:

before-replacement-with-regular-expressions-aspose-words-cpp

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

after-replacement-with-regular-expressions-aspose-words-cpp

메타 문자를 사용하여 문자열 찾기 및 바꾸기

특정 텍스트 또는 구가 여러 단락,섹션 또는 페이지로 구성된 경우 검색 문자열 또는 대체 문자열에 메타 문자를 사용할 수 있습니다. 메타 문자 중 일부는 다음과 같습니다. &p 단락 나누기, &b 섹션 휴식, &m 페이지 나누기,그리고 &l 줄 바꿈을 위해.

다음 코드 예제에서는 텍스트를 단락 및 페이지 나누기로 바꾸는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->get_Font()->set_Name(u"Arial");
builder->Writeln(u"First section");
builder->Writeln(u" 1st paragraph");
builder->Writeln(u" 2nd paragraph");
builder->Writeln(u"{insert-section}");
builder->Writeln(u"Second section");
builder->Writeln(u" 1st paragraph");
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
options->get_ApplyParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
// Double each paragraph break after word "section", add kind of underline and make it centered.
int32_t count = doc->get_Range()->Replace(u"section&p", u"section&p----------------------&p", options);
// Insert section break instead of custom text tag.
count = doc->get_Range()->Replace(u"{insert-section}", u"&b", options);
System::String savePath = outputDataDir + u"FindReplaceUsingMetaCharacters.ReplaceTextContaingMetaCharacters.docx";
doc->Save(savePath);

당신은 찾아 사용하여 워드 문서의 머리글/바닥 글 섹션에서 텍스트를 대체 할 수 있습니다 HeaderFooter 수업

다음 코드 예제에서는 문서에서 머리글 섹션의 텍스트를 대체하는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Open the template document, containing obsolete copyright information in the footer.
auto doc = System::MakeObject<Document>(inputDataDir + u"HeaderFooter.ReplaceText.doc");
// Access header of the Word document.
auto headersFooters = doc->get_FirstSection()->get_HeadersFooters();
auto header = headersFooters->idx_get(HeaderFooterType::HeaderPrimary);
// Set options.
auto options = System::MakeObject<FindReplaceOptions>();
options->set_MatchCase(false);
options->set_FindWholeWordsOnly(false);
// Replace text in the header of the Word document.
header->get_Range()->Replace(u"Aspose.Words", u"Remove", options);
auto footer = headersFooters->idx_get(HeaderFooterType::FooterPrimary);
footer->get_Range()->Replace(u"(C) 2006 Aspose Pty Ltd.", u"Copyright (C) Aspose Pty Ltd.", options);
// Save the Word document.
doc->Save(outputDataDir + u"HeaderReplace.docx");

헤더 문자열 대체를 적용하기 전에 문서 간의 차이점을 확인할 수 있습니다:

before-applying-header-string-replacement-aspose-words-cpp

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

after-applying-header-string-replacement-aspose-words-cpp

문서에서 바닥글 섹션의 텍스트를 대체하는 코드 예제는 이전 머리글 코드 예제와 매우 유사합니다. 다음 두 줄을 바꾸기 만하면됩니다:

auto header = headersFooters->idx_get(HeaderFooterType::HeaderPrimary);
header->get_Range()->Replace(u"Aspose.Words", u"Remove", options);

다음 과 함께:

auto footer = headersFooters->idx_get(HeaderFooterType::FooterPrimary);
footer->get_Range()->Replace(u"(C) 2006 Aspose Pty Ltd.", u"Copyright (C) Aspose Pty Ltd.", options);

바닥글 문자열 대체를 적용하기 전에 문서의 차이점을 확인할 수 있습니다:

before-applying-footer-string-replacement-aspose-words-cpp

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

after-applying-footer-string-replacement-aspose-words-cpp

찾기 및 바꾸기 중 텍스트 무시

찾기 및 바꾸기 작업을 적용하는 동안 텍스트의 특정 세그먼트를 무시할 수 있습니다. 따라서 텍스트의 특정 부분을 검색에서 제외 할 수 있으며 찾기 및 바꾸기는 나머지 부분에만 적용 할 수 있습니다.

Aspose.Words 다음과 같은 텍스트를 무시하기 위한 많은 찾기 및 바꾸기 속성을 제공합니다 IgnoreDeleted, IgnoreFieldCodes, IgnoreFields, IgnoreFootnotes,그리고 IgnoreInserted.

다음 코드 예제에서는 수정본 삭제 내에서 텍스트를 무시하는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Insert non-revised text.
builder->Writeln(u"Deleted");
builder->Write(u"Text");
// Remove first paragraph with tracking revisions.
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now());
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->Remove();
doc->StopTrackRevisions();
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Replace 'e' in document while deleted text.
options->set_IgnoreDeleted(true);
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options);
std::cout << doc->GetText().ToUtf8String() << std::endl; // The output is: Deleted\rT*xt\f
// Replace 'e' in document NOT ignoring deleted text.
options->set_IgnoreDeleted(false);
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options);
std::cout << doc->GetText().ToUtf8String() << std::endl; // The output is: D*l*t*d\rT*xt\f

찾기 및 바꾸기 작업 사용자 지정

Aspose.Words 많은 다른 제공합니다 properties 특정 형식 적용과 같은 텍스트를 찾아서 바꾸려면 ApplyFont 그리고 ApplyParagraphFormats 속성,대체 패턴에서 대체를 사용하여 UseSubstitutions 재산 및 기타.

다음 코드 예제에서는 문서에서 특정 단어를 강조 표시하는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Highlight word "the" with yellow color.
auto options = System::MakeObject<FindReplaceOptions>();
options->get_ApplyFont()->set_HighlightColor(System::Drawing::Color::get_Yellow());
// Replace highlighted text.
doc->get_Range()->Replace(u"Hello", u"Hello", options);

Aspose.Words 당신이 사용할 수 있습니다 IReplacingCallback 바꾸기 작업 중에 사용자 지정 메서드를 만들고 호출하는 인터페이스입니다. 정규식으로 지정된 텍스트를 다음과 같이 바꾸려면 찾기 및 바꾸기 작업을 사용자 지정해야 하는 몇 가지 사용 사례가 있을 수 있습니다 HTML 태그,그래서 기본적으로 당신은 삽입으로 대체 적용됩니다 HTML.

문자열을 다음과 같이 바꾸어야 하는 경우 HTML 태그,적용 IReplacingCallback 문서의 일치 노드와 실행 시작 시 일치가 시작되도록 찾기 및 바꾸기 작업을 사용자 지정하는 인터페이스입니다. 우리가 사용하는 몇 가지 예를 제공하자 IReplacingCallback.

다음 코드 예제에서는 지정된 텍스트를 다음과 같이 바꾸는 방법을 보여 줍니다 HTML:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
class ReplaceWithHtmlEvaluator : public IReplacingCallback
{
typedef ReplaceWithHtmlEvaluator ThisType;
typedef IReplacingCallback BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo);
public:
ReplaceWithHtmlEvaluator(System::SharedPtr<FindReplaceOptions> options);
ReplaceAction Replacing(System::SharedPtr<ReplacingArgs> args) override;
private:
System::SharedPtr<FindReplaceOptions> mOptions;
};
ReplaceWithHtmlEvaluator::ReplaceWithHtmlEvaluator(System::SharedPtr<FindReplaceOptions> options)
{
mOptions = options;
}
ReplaceAction ReplaceWithHtmlEvaluator::Replacing(System::SharedPtr<ReplacingArgs> args)
{
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(System::DynamicCast<Document>(args->get_MatchNode()->get_Document()));
builder->MoveTo(args->get_MatchNode());
// Replace '<CustomerName>' text with a red bold name.
builder->InsertHtml(u"<b><font color='red'>James Bond, </font></b>");
args->set_Replacement(u"");
return ReplaceAction::Replace;
}
void ReplaceWithHtml(System::String const& inputDataDir, System::String const& outputDataDir)
{
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello <CustomerName>,");
auto options = System::MakeObject<FindReplaceOptions>();
auto optionsReplacingCallback = System::MakeObject<ReplaceWithHtmlEvaluator>(options);
doc->get_Range()->Replace(new Regex(u" <CustomerName>,"), System::String::Empty, options);
// Save the modified document.
doc->Save(outputDataDir + u"Range.ReplaceWithInsertHtml.doc");
}

다음 코드 예제에서는 녹색으로 양수를 강조 표시하고 빨간색으로 음수를 강조 표시하는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Replace and Highlight Numbers.
class NumberHighlightCallback : public IReplacingCallback
{
typedef NumberHighlightCallback ThisType;
typedef IReplacingCallback BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo);
public:
NumberHighlightCallback(System::SharedPtr<FindReplaceOptions> const& opt)
: mOpt(opt) { }
Aspose::Words::Replacing::ReplaceAction Replacing(System::SharedPtr<Aspose::Words::Replacing::ReplacingArgs> args) override
{
// Let replacement to be the same text.
args->set_Replacement(args->get_Match()->get_Value());
auto val = System::Convert::ToInt32(args->get_Match()->get_Value());
// Apply either red or green color depending on the number value sign.
mOpt->get_ApplyFont()->set_Color(val > 0 ? System::Drawing::Color::get_Green() : System::Drawing::Color::get_Red());
return ReplaceAction::Replace;
}
private:
System::SharedPtr<FindReplaceOptions> mOpt;
};

다음 코드 예제에서는 각 줄에 줄 번호를 앞에 붙이는 방법을 보여 줍니다:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
class LineCounterCallback : public IReplacingCallback
{
typedef LineCounterCallback ThisType;
typedef IReplacingCallback BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo);
public:
Aspose::Words::Replacing::ReplaceAction Replacing(System::SharedPtr<Aspose::Words::Replacing::ReplacingArgs> args) override
{
std::cout << args->get_Match()->get_Value().ToUtf8String() << '\n';
args->set_Replacement(System::String::Format(u"{0} {1}", mCounter++, args->get_Match()->get_Value()));
return ReplaceAction::Replace;
}
private:
int32_t mCounter = 1;
};
void LineCounter(System::String const& inputDataDir, System::String const& outputDataDir)
{
// Create a document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Add lines of text.
builder->Writeln(u"This is first line");
builder->Writeln(u"Second line");
builder->Writeln(u"And last line");
// Prepend each line with line number.
auto opt = System::MakeObject<FindReplaceOptions>();
opt->set_ReplacingCallback(System::MakeObject<LineCounterCallback>());
doc->get_Range()->Replace(System::MakeObject<Regex>(u"[^&p]*&p"), u"", opt);
doc->Save(outputDataDir + u"TestLineCounter.docx");
}