使用正则表达式创建分隔文本

xqui 发布于 2019-03-09 excel 最后更新 2019-03-09 14:40 2 浏览

我有一个词汇表,其中的条目以粗体文本形式给出,说明文字是常规字体。我想要做的是在每一串粗体文本之后添加一个逗号(星号或任何其他标点符号),然后使我能够将术语表转换为Excel中的逗号分隔文本。有没有办法在Word的查找和替换对话框中使用正则表达式来执行此操作,以便在术语表中的词条或词组末尾可以使用逗号。以词汇表中的一个条目为例: 在生效时废止....正式字体的内容 该条目是粗体短语,与其相关的解释以常规文本给出。 在尝试表达式< [A-Za-z \,.-)(/ \ ?!] {1,}> Jerry在查找框中提示以及^&;在MS Word中的替换框中后,粗体短语所需的结果,然后是段落中断,例如标题和标题:


http://img811.imageshack.us/img811/6338/frontpagep.jpg
但词汇表条目没有发生变化,因为它们后面跟着条目的内容,在它们之后没有段落中断。以下是词汇表中的一个示例,显示词汇表条目的特征布局:
http://img571.imageshack.us/img571/6558/samplefromtheglossary.jpg
已邀请:

xipsam

赞同来自:

使用通配符,将格式字体设置为粗体。要进行搜索,请输入<*>。要进行替换,请输入^&,Picture is worth a thousand scripts

et_id

赞同来自:

一些简单的搜索显示以下文档适用于至少2002年的旧版Word。 office support document 请注意,Word的Regex实现与其他人的实现不太一致。 我没有看到更新版Word的文档。 Visual Studio中的搜索和替换(我认为这也适用于免费版本),或者像Eclipse这样的IDE中支持Regex,因此您可以使用许多非Word选项。

zhic

赞同来自:

你能试试这个: <[A-Za-z\,.-)(\/\?! ]{1,}> 而不是之前提到的乔治建议的<*>? 在对OP进行新的编辑之后: 您可以尝试将其放在宏中:

Sub CommaAdder()
    Selection.Find.ClearFormatting
    Selection.Find.Font.Bold = True
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "<[A-Za-z]@>"
        .Replacement.Text = "^&,"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = ",([\)])"
        .Replacement.Text = "\1,"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = ",([\-\?\/\!\.\, ])"
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = "([A-Za-z]@ )"
        .Replacement.Text = "\1,"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = "( \,)([A-Za-z]@)"
        .Replacement.Text = ", \2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = "\,\("
        .Replacement.Text = "("
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = "\, "
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    With Selection.Find
        .Text = " \,"
        .Replacement.Text = ", "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
End Sub
我不知道如何编写宏,但是我记录了一个替换来构建它,如果你理解了一点,就会有3个替换继续进行。第一个查找所有粗体单词并在它们之间加上逗号,即使有).等。第二个专门查看我刚刚提到的删除该逗号的实例,以及我们有逗号后跟的逗号一个醒目的空白区域,,除了,)部分,它在最后一次替换中被寻址,而是替换为),。 问题是如果你有类似的东西:
This is bold but not this
boldbut之间的空格也具有bold格式,它将在第二次替换时删除。如果有一种方法可以找到部分为粗体而部分不是粗体的文本,则不会有任何问题。我正在尝试寻找解决方案,但如果此代码存在任何问题,请告诉我。如果没有像这样格式化为粗体的空格,则不会有任何问题! reedIT:现在也适用于大胆的空间!虽然它不太整洁......