2

I want to know the number of times that any year from 1900 through the current year is mentioned in a Word file.

I know how to use a wildcard expression to do this in two steps as follows:
<(19[0-9]{2})> to find matches 1900-1999
<(20[0-9]{2})> to find matches 2000-2099

What is the syntax to reduce this to one step?

In case it makes a difference, I am using Word 2010.

Reason for Question When I estimate the effort to edit academic papers, I want to factor in the sheer number of in-text citations that need to be "eyeballed" for missing commas, parentheses, and such.

The sheer number needs to be one I have high confidence in; the number does not have to be exact.

  • Try <([19|20][0-9]{2})> Does that help? – der_michael Aug 20 '15 at 23:04
  • Unfortunately, it did not work. – RJo Aug 21 '15 at 0:45
0

Best you could do is look for 4 digit numbers starting with 1 or 2 <[1-2][0-9]{3}> but how accurate that is depends very much on the content of the document.

  • Peter, it picks up dates in the 1700s and 1800s. – RJo Aug 23 '15 at 20:17
0

By studying the answers from der_michael (above) and Peter (below), I was able to combine their advice to arrive at this answer:

<[1920]{2}[0-9]{2}>

So far I've tried the syntax with three papers, with the number of in-text citations ranging from 300-731. The number of false positives were acceptable (1, 2, and 2).

  • [12][0-9][0-9][0-9]> is slightly better, but still not good since it matches 2999 too. Don't use <. Use a normal space instead. In my test file < also matches negative numbers like -1999. Word's wildcard search doesn't support OR-expression | which would solve the problem. Anyway, a VBA regex version is your best shot – nixda Aug 23 '15 at 21:42
0

This is a Regular expression to match all years between 1900 and 2015

\s((19[0-9][0-9])|(200[0-9])|(201[0-5]))\s
  • The basic structure is ((first_range) OR (second_range) OR (third_range))
  • We use a logical OR | to allow three ranges
    • (19[0-9][0-9] goes from 1900 - 1999
    • (200[0-9]) goes from 2000 - 2009
    • (201[0-5]) goes from 2010 - 2015
  • \s looks for whitespace before and after a match. This prevents negative numbers

Unfortunately, you cannot use this pattern in Word's Find & Replace dialog. You have to open your VBA editor (ALT+F11) and paste the below macro. Execute it with F5

An input window will pop up where you enter your RegEx pattern. The macro counts all occurrences and highlight them if you choose to

Sub RegexReplace()

    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    'On Error Resume Next

    RegEx.Global = True
    RegEx.Pattern = InputBox("Enter your regex pattern to find and count")
    Set Matches = RegEx.Execute(ActiveDocument.Range)

    answer = MsgBox("There are " & Matches.Count & " occurances" & vbCrLf & _
        "Highlight them?", vbYesNo)

    If answer = vbYes Then
        For Each hit In Matches
           ActiveDocument.Range(hit.FirstIndex, hit.FirstIndex + hit.Length). _
             HighlightColorIndex = wdYellow
        Next hit
    End If

End Sub

Result in Word

enter image description here

Used resources

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.