Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #1  
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
  #2  
Old 01-03-2025, 11:33 AM
gmaxey gmaxey is offline Regex-pattern Windows 10 Regex-pattern Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Batmat1,


Yes, correct. Omitted space after first comma was typo. Now, testing with your latest version: ", [A-Z][^, ]+( [^, ]+)*( [A-Z][^,]+)?,"
when applied to the following example returns the same matches as my last version: ",\s{A-Z][^,]*,"



Specifically the last instance ", John smith," is returned as a match. How could we prevent that? If you don't mind, can you explain what each part of your pattern is intended to perform?


For others following, with mine it is
1. "," match a comma
2. "\s" match a space
3. "[A-Z]" match a capital letter A to Z
4. "[^,]*" match any characters excluding a comma one or more times
5. "," match a comma
Attached Images
File Type: jpg Example.jpg (92.8 KB, 19 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
  #3  
Old 01-03-2025, 02:36 PM
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
Batmat1,


Specifically the last instance ", John smith," is returned as a match. How could we prevent that?

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
The author of the thread did not really provide criteria for the input data. If we have requirements for the form of the results, we must also specify the form of the input data. If the input data can be any, we must provide all characters accepted between commas. See that the given code finds the result ", Beate 123-van4 Ackeren," and that is not a surname and name, right?

The code below accepts only characters in CONST characters. Tested with data as in the picture.
Code:
Sub ScratchMacro()
Const characters As String = "[A-Za-zü\-]"
Dim RegEx As Object, Matches As Object, Match As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Global = True
        .Pattern = ", [A-Z]" & characters & "+(|( " & characters & "+)* [A-Z]" & characters & "+),"
    End With

    Set Matches = RegEx.Execute(ActiveDocument.Range.text)
    For Each Match In Matches
        Debug.Print Match.Value
    Next
End Sub

Quote:

If you don't mind, can you explain what each part of your pattern is intended to perform?


For others following, with mine it is
1. "," match a comma
2. "\s" match a space
3. "[A-Z]" match a capital letter A to Z
4. "[^,]*" match any characters excluding a comma one or more times
5. "," match a comma
2. "\s" match a comma, TAB, form-feed, .... equivalent with "[ \f\n\r\t\v]"
Attached Images
File Type: png regex.png (37.2 KB, 26 views)
Closed Thread



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 11:10 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