Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #10  
Old 01-03-2025, 10:19 AM
batman1 batman1 is offline Regex-pattern Windows 11 Regex-pattern Office 2013
Advanced Beginner
 
Join Date: Jan 2025
Posts: 57
batman1 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Batman1,


Thanks for the post. I see that your suggested pattern "requires" two or more words between the commas to return a match. So


, John, won't match but
, John smith, would.

If you read post #1, #2, #3, #4 you will understand why I wrote: Last name-first name with 2, 3, 4 ... parts, so there should be at least 2 parts.


Quote:

A RegEx pattern master I am not. I wonder if it is possible to construct the pattern such that the last word (the word before the ending comma) must be captitalized e.g.,


,Gerd van Ackerman, matches
,Gerd van ackeramn, would not
With my code ",Gerd van Ackerman," does NOT match. After the first comma there MUST be a space. This is a requirement of this thread.

The following code meets your requirements. I can't write a simpler pattern.

Code:
Sub ScratchMacro()
Dim RegEx As Object, Matches As Object, Match As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Global = True
        .Pattern = ", [A-Z][^, ]+( [^, ]+)*( [A-Z][^,]+)?,"
    End With
    Set Matches = RegEx.Execute(ActiveDocument.Range.text)
    For Each Match In Matches
        Debug.Print Match.Value
    Next
End Sub
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex-pattern Capture group in RegEx alex100 Word VBA 1 01-02-2021 02:39 PM
Regex-pattern Regex over 700 matches in a long doc totoMSOF Word VBA 19 03-11-2019 01:28 PM
Regex-pattern 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
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 01:43 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