Thread: [Solved] Regex-pattern
View Single Post
 
Old 01-02-2025, 10:14 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Vivka,


Thank you for your kind words. I was not able to get you suggestion to work. While it will match:


Test, Joe Smith, and test, test.
or
Test, Bob Miller, and test, text.
It will not match any of the examples given in post #3.


Consider:
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strPattern As String
Dim RegEx As RegExp, Matches As Object, Match As Object
Dim oPar As Paragraph
  'strPattern = ",(\s[A-Z][a-z]+)+," 'Vivka's
  strPattern = ",\s[A-Z][^,]*," 'Revised
  Set RegEx = CreateObject("vbscript.regexp")
  For Each oPar In ActiveDocument.Range.Paragraphs
    With RegEx
      .Pattern = strPattern
      Set Matches = RegEx.Execute(oPar.Range.Text)
      For Each Match In Matches
        Debug.Print Match.Value
      Next
    End With
  Next oPar
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/