regex - Inserting a field with find and replace in a word document - Stack Overflow

admin2025-04-21  2

I've got a bunch of regulations, and I have a regular expression that finds them. They all look like "28-35-###(sometimes letter here)". I have all of them as bookmarked as the text KAR28_35_###(sometimes letter here).

I would like to find all instances of the text K.A.R. 28-35-###(a-z)? and tack on a "[p. { PAGEREF KAR28_35_###(a-z)? \h }]" on the end so it ends up looking like "K.A.R. 28-35-144a [p. 35]."

But when I do find/replace, I can't insert the field curlycues, just regular curly brackets. It's only text.

Is there a way I can get it to insert an actual field?

I've got a bunch of regulations, and I have a regular expression that finds them. They all look like "28-35-###(sometimes letter here)". I have all of them as bookmarked as the text KAR28_35_###(sometimes letter here).

I would like to find all instances of the text K.A.R. 28-35-###(a-z)? and tack on a "[p. { PAGEREF KAR28_35_###(a-z)? \h }]" on the end so it ends up looking like "K.A.R. 28-35-144a [p. 35]."

But when I do find/replace, I can't insert the field curlycues, just regular curly brackets. It's only text.

Is there a way I can get it to insert an actual field?

Share Improve this question edited Jan 24 at 20:46 ruud 1,0334 gold badges19 silver badges32 bronze badges asked Jan 22 at 22:15 AccessUserAccessUser 11 bronze badge 2
  • 1 so maybe post the regex you're using and some sample data? – danielRICADO Commented Jan 22 at 22:20
  • Apologies. the regex was finding things correctly. The problem was not being able to insert a field. I can put { PAGEREF KAR_28_35_144a ^92h } as the replacement, but {} is just text, not a field. I can put ^c in the replacement and hold a field in my clipboard while doing it, but that would insert the same field for all instances found. I need the last three or four digits in KAR_28_35_###a to change to match the text I'm finding. – AccessUser Commented Jan 23 at 17:40
Add a comment  | 

1 Answer 1

Reset to default 0

Regex can only work with text strings, not formatting or other non-text data such as field braces. You could, however, use a Word macro like:

Sub InsertPageRefs()
Application.ScreenUpdating = False
Dim StrTxt As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "K.A.R. 28-35-[0-9a-z]@>"
    .Replacement.Text = ""
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Forward = True
    .Format = False
  End With
  Do While .Find.Execute
    StrTxt = "PAGEREF KAR_28_35_" & Split(.Text, "-")(2) & " \h"
    With .Duplicate
      .Collapse wdCollapseEnd
      .Text = " [p.  ]"
      .Fields.Add Range:=.Characters.Last.Previous, Type:=wdFieldEmpty, Text:=StrTxt, PreserveFormatting:=False
    End With
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub

To make the code even more flexible, so that it finds all the K.A.R. ##-##-###? sequences, you could use:

    .Text = "K.A.R. [0-9]{2}-[0-9]{2}-[0-9a-z]@>"

or, for sequences like K.A.R. #-##-###? and K.A.R. ##-#-###? as well:

    .Text = "K.A.R. [0-9]@-[0-9]@-[0-9a-z]@>"

and:

    StrTxt = "PAGEREF KAR_" & Replace(Split(.Text, " ")(1), "-", "_") & " \h"
转载请注明原文地址:http://anycun.com/QandA/1745226272a90467.html