WORD-通过引号中的“文本"查找和替换文本 [英] WORD - Find and replace text via Text in quotation marks

查看:203
本文介绍了WORD-通过引号中的“文本"查找和替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在引号文本之间查找和修剪空格?

How Can I find and trim spaces between quotation text?

例如:如果单词包含以下字符串: 我对他说'我为什么要呢?他回答... 它将替换: 我对他说:为什么?他回答...

for example: if the word contains the following string: I say to him ' why should I? ' he answers... It will replace: I say to him 'why should I?' he answers...

我知道在引号中查找文本的正则表达式为:(\'*?\'),但从这里开始我无法继续前进.

I know that the regular expression to find text in the quotation is:(\'*?\') but from here I could not progress.

任何帮助将不胜感激 阿西

Any help will be highly appreciated Asi

推荐答案

对于VBA解决方案,请尝试:

For a VBA solution, try:

Sub Demo()
Dim Rng As Range, Rslt
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[‘'][!^13^l^t]@['’]"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    .Select
    Set Rng = .Duplicate
    With Rng
      .Start = .Start + 1
      .End = .End - 1
      If .Text <> Trim(.Text) Then
        Rslt = MsgBox("Trim this instance?", vbYesNoCancel)
        If Rslt = vbCancel Then Exit Sub
        If Rslt = vbYes Then
          Do While .Characters.Last = " "
            .Characters.Last = vbNullString
          Loop
          Do While .Characters.First = " "
            .Characters.First = vbNullString
          Loop
        End If
      End If
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub

注意:如果字符串未应用任何格式,则可以减少:

Note: If the strings don't have any formatting applied, you could reduce:

  Do While .Characters.Last = " "
    .Characters.Last = vbNullString
  Loop
  Do While .Characters.First = " "
    .Characters.First = vbNullString
  Loop

收件人:

  .Text = Trim(.Text)

要仅在选定范围内工作,请更改:

To work with just a selected range, change:

Dim Rng As Range, Rslt
With ActiveDocument.Range

收件人:

Dim Rng As Range, RngSel As Range, Rslt
With Selection.Range
  Set RngSel = .Duplicate

并插入:

    If .InRange(RngSel) = False Then Exit Sub

之前:

    .Select

这篇关于WORD-通过引号中的“文本"查找和替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆