我想在右边找到en dash的内容

时间:2017-10-31 01:07:03

标签: regex vba

Image

我有两组数据:

1977, 74, 5716-5720, doi:10413454   
1967, 8, 8323, doi:10413454   

我想把第二组,没有冲破这组标有黄色的数据。我使用正则表达式\d{4}, \d+, \d+(?!-)

但这不起作用。我哪里错了?

Sub Yellow()   
    Dim p As Paragraph, d As Document, reg As Object  
    Set d = ActiveDocument  
    Set reg = CreateObject("VBscript.Regexp")  
    reg.IgnoreCase = False  
    reg.Global = True   
    reg.Pattern = "\d{4}, \d+, \d+(?!-)"   
    For Each p In d.Paragraphs   
        If reg.Execute(p.Range.Text).count = 1 Then  
            m = reg.Execute(p.Range.Text)(0).FirstIndex  
            n = reg.Execute(p.Range.Text)(0).Length   
            With d.Range(p.Range.Start + m + 9, p.Range.Start + m + n)    
                .HighlightColorIndex = wdYellow   
                End With  
                End If    
        Next
End Sub

2 个答案:

答案 0 :(得分:0)

应用于输入

1977, 74, 5716-5720, doi:10413454 

你的正则表达式\d{4}, \d+, \d+(?!-)匹配1977, 74, 571

使用word boundary anchors确保数字始终完全匹配:

\b\d{4}, \d+, \d+\b(?!-)

答案 1 :(得分:0)

selectedIndex