#1
|
|||
|
|||
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 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 |
#2
|
||||
|
||||
If I understand your Regex correctly - and what you're trying to achieve - it's equivalent to a wildcard Find/Replace, where:
Find = \[\|\]^13[!^13]@^13[!^13]@\|^13 Replace = -------------^&
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regex over 700 matches in a long doc | totoMSOF | Word VBA | 19 | 03-11-2019 01:28 PM |
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 |
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 |