Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-02-2021, 05:47 AM
alex100 alex100 is offline Capture group in RegEx Windows 7 64bit Capture group in RegEx Office 2016
Advanced Beginner
Capture group in RegEx
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Capture group in RegEx

I am using the code below to find and replace some text. I uses a RegEx that has two groups "(.*?)".



Code:
Dim objRegex As regExp
Dim matches As MatchCollection
Dim fnd As Match
Set objRegex = New regExp
With objRegex
    .Pattern = "\[\|\]" & vbCr & "(.*?)" & vbCr & "(.*?)" & "\|" & vbCr
    .Global = True
    .IgnoreCase = True
    Set matches = .Execute(Selection.Text)
End With
With Selection
    .HomeKey wdStory
    With .Find
        .ClearFormatting
        .Forward = True
        .Format = False
        .MatchCase = True
        For Each fnd In matches
            .Text = fnd
            .Execute
            If .Found = True Then
                Selection.TypeText Text:="-------------"
            End If
        Next fnd
    End With
 End With
The RegEx works good.

I do have a problem though... how can I capture the first group in the RegEx and add it before the replacement text (which is this line: "-------------")?

Currently, the replacement text is added using the 'Selection.TypeText' instruction, as you can see. That's because I couldn't find the way to use the '.Replacement.Text' instruction for this particular code. I tried using it, but nothing seemed to work.

Anyhow, I guess the solution could be something like this:

.Replacement.Text="$1-------------"

where $1 represents the first captured group in the RegEx. This is just an example that should help you better understand what I'm looking for.

Alex
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Capture group in RegEx Regex over 700 matches in a long doc totoMSOF Word VBA 19 03-11-2019 01:28 PM
Capture group in RegEx Using VB Regex feature, I tried to replace 'the' and 'this' with 'that' but got screwed abdan Word VBA 3 01-18-2019 09:38 PM
Macro help regex subspace3 Word VBA 1 10-15-2014 09:53 AM
Capture group in RegEx Convert RegEx to Word (Devanagari Font Find/Replace) gasyoun Word VBA 9 04-12-2013 04:15 PM
Regex in Word: Replaced strings are in disorder chgeiselmann Word 0 04-26-2009 11:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft