在Word 2013中,匹配文档开头的通配符

时间:2014-10-27 06:47:46

标签: regex vba ms-word word-vba

在Word 2013中,在启用或不启用通配符的情况下使用“搜索和替换”,我想在Hello的段落开头替换Bye的每个匹配项。

搜索模式:

  

^ pHello

仅适用于非第一行,并且与文档第一段开头的Hello不匹配。

如何在文档开头匹配Hello?在Perl中,这将以s/^Hello/Bye/完成。

1 个答案:

答案 0 :(得分:1)

匹配文档开头的通配符似乎不存在。

我所做的是在文档的开头添加段落标记,进行搜索,然后删除段落标记。以下是Perl中的样子:

my $word = Win32::OLE->new ('Word.Application', 'Quit') or die $!;

$word->Selection->HomeKey ({Unit => wdStory}); # to the beginning of the doc
$word->Selection->TypeText ({Text => "\n"}); # add the ^p
$word->Selection->HomeKey ({Unit => wdStory}); # to the beginning of the doc

my $search = $document->Content->Find; 

$search->{Text}              = "^pHello";
$search->Replacement->{Text} = "^pBye";
$search->Execute ();

$word->Selection->Delete; # delete the ^p
相关问题