Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #16  
Old 01-05-2025, 07:11 AM
vivka vivka is offline Regex-pattern Windows 7 64bit Regex-pattern Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default


Yes, Batman1, you are right! Sorry for my inattentive reading posts!
  #17  
Old 01-05-2025, 11:41 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 vivka View Post
Yes, Batman1, you are right! Sorry for my inattentive reading posts!
In fact, it doesn't have to be "|", and the code can be like this
Code:
Sub ScratchMacro()
Dim RegEx As Object, Matches As Object, Match As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Global = True
        .Pattern = ",( [A-Za-zü\-]+)* [A-Z][A-Za-zü\-]+,"
    End With

    Set Matches = RegEx.Execute(ActiveDocument.Range.text)
    For Each Match In Matches
        Debug.Print Match.Value
    Next
End Sub
  #18  
Old 01-05-2025, 12:17 PM
gmaxey gmaxey is offline Regex-pattern Windows 10 Regex-pattern 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

batman1,


Obviously you have a better understanding of RegEx patterns than I do. For my purposes, both of your last suggestions worked.


Can you explain their construction? How do they work?


I can follow how either pattern returns a match when a two part name is between the commas e.g.,

Test, Jack Box, end test, test.
or three or more part names e.g.,
Test, Jack in the Box, end test, test.


But how does this pattern ",( [A-Za-zü\-]+)* [A-Z][A-Za-zü\-]+," (or the other) return a match when there is just one name between the commas e.g.,


Test, Jack, end test, test.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
  #19  
Old 01-05-2025, 01:11 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
batman1,

But how does this pattern ",( [A-Za-zü\-]+)* [A-Z][A-Za-zü\-]+," (or the other) return a match when there is just one name between the commas e.g.,


Test, Jack, end test, test.
In the case of "Test, Jack, end test, test." we have ", Jack," = ", " & A & B & "," - where A = "", B = " Jack"

A = "" means ( [A-Za-zü\-]+) occurs 0 times (* means the expression before it occurs 0 or many times). It doesn't matter what the expression but occurs 0 times it gives an empty string.

B = " Jack" = " J" & "ack" = " [A-Z]" & 3 times "[A-Za-zü\-]"
  #20  
Old 01-05-2025, 01:21 PM
gmaxey gmaxey is offline Regex-pattern Windows 10 Regex-pattern 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

In a slight fog, but I think I follow. Thank you and thank you for your participation in this forum and thread.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
  #21  
Old 01-06-2025, 05:56 AM
vivka vivka is offline Regex-pattern Windows 7 64bit Regex-pattern Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Batman's final code with the capturing group moved to the end (my personal opinion: similar effect, but better for understanding the logic):
Code:
, [A-Z][A-Za-zü\-]+( [A-Za-zü\-]+)*,
Or a little shorter:
Code:
, [A-Z]([ A-Za-zü\-])*,
Although, longer codes include more conditions, those conditions may be very rare to occur.
P.S. Naturally, making "improvements" to the basic code (Batman's) is less demanding than making the basic code.
  #22  
Old 01-06-2025, 06:44 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 vivka View Post
Sometimes regex is more functional than Word standard instruments.
That's right. For example, can you provide a simple pattern in Word Find with the following task: "Find in the sequence all numbers consisting of 4 or 5 digits, whose first 4 digits must have at least 1 digit 2"
  #23  
Old 01-06-2025, 06:50 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 vivka View Post
Or a little shorter:
Code:
, [A-Z]([ A-Za-zü\-])*,
In post #15 Greg's wrote: Specifically the last instance ", John smith," is returned as a match. How could we PREVEN T that?" So Greg's wants to find "John Smith," but he doesn't want to find "John smith,"

Your code will find ", John smith," and that's not what Greg wants.

I also want a simpler pattern, but for now I've provided a pattern that at least meets Greg's requirements.
  #24  
Old 01-06-2025, 07:03 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 vivka View Post
Batman's final code with the capturing group moved to the end (my personal opinion: similar effect, but better for understanding the logic):
Code:
, [A-Z][A-Za-zü\-]+( [A-Za-zü\-]+)*,

I don't understand. ", [A-Z][A-Za-zü\-]+( [A-Za-zü\-]+)*," it's not my code.


My code is ",( [A-Za-zü\-]+)* [A-Z][A-Za-zü\-]+,"


SIMILAR EFFECT?

Kod ", [A-Z][A-Za-zü\-]+( [A-Za-zü\-]+)*," znajdzie ", John smith,"


My code ",( [A-Za-zü\-]+)* [A-Z][A-Za-zü\-]+," NIE znajdzie ", John smith,"
  #25  
Old 01-06-2025, 07:24 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 vivka View Post
Although, longer codes include more conditions, those conditions may be very rare to occur.
P.S. Naturally, making "improvements" to the basic code (Batman's) is less demanding than making the basic code.
My code is a proposal for SPECIFIC requirements, including Greg's. I didn't come up with these requirements, others did. And since I answered them, I have to give code that meets their requirements
  #26  
Old 01-06-2025, 08:38 AM
vivka vivka is offline Regex-pattern Windows 7 64bit Regex-pattern Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Yes, Batman1, your code meets all current requirements. In current situation, my "improvent" is actually a deterioration. But I tried to be helpful!
  #27  
Old 01-06-2025, 09:27 AM
gmaxey gmaxey is offline Regex-pattern Windows 10 Regex-pattern 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

Quote:
Originally Posted by batman1 View Post
That's right. For example, can you provide a simple pattern in Word Find with the following task: "Find in the sequence all numbers consisting of 4 or 5 digits, whose first 4 digits must have at least 1 digit 2"

batman1,


I suppose the skills that someone is familiar with has bearing on what is simple or not. A simple find pattern? No, and one may not exists. However, with VBA your task is easy enough:



Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "[0-9]{1,}"
    .MatchWildcards = True
    While .Execute
      Select Case Len(oRng.Text)
        Case 4
          If InStr(oRng.Text, "2") > 0 Then Debug.Print oRng.Text
        Case 5
          If InStr(Left(oRng.Text, 4), "2") > 0 Then Debug.Print oRng.Text
      End Select
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
Sample Numbers:
12345 – Match
56789 – No Match
123 – No Match
1234 – Match
4321 – Match
8945 – No Match
98765 – No Match
124567 – No match
54312 – No match
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
  #28  
Old 01-06-2025, 11:04 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,


I suppose the skills that someone is familiar with has bearing on what is simple or not. A simple find pattern? No, and one may not exists. However, with VBA your task is easy enough:

1. There is nothing that says that a number that has 4 or 5 digits has to be a word. So it can be part of a word. Besides, your code will find 3452 in the string "a3452b" (and correctly), so it should find 12456 (1245 also meets the criteria, but we want the longest result possible) in the string "124567".

2. Every case, well almost every case, can be solved in VBA. The only thing is that the solution is relatively short. In the current code, after .Execute you have to use Select.


Besides, if data = "13452234567" the code will not find 34522
  #29  
Old 01-06-2025, 11:16 AM
vivka vivka is offline Regex-pattern Windows 7 64bit Regex-pattern Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Or another presentation of Greg's idea:
Code:
Sub ScratchMacro()
'Coded by Gregory K. Maxey
'https://www.msofficeforums.com/184580-post31.html

 Dim oRng As range
  Set oRng = ActiveDocument.range
  With oRng.Find
    .text = "<[0-9]{4,5}>"
    .MatchWildcards = True
    While .Execute
        If InStr(oRng.text, "2") > 0 Then MsgBox oRng.text
    Wend
  End With
lbl_Exit:
  Exit Sub
  End Sub
However I had situations when regex helped me out, unlike VBA. Maybe it was because of my poor VBA knowledge.
Greg and Batman1, thank you for another good lesson!
  #30  
Old 01-06-2025, 11:33 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 vivka View Post
Or another presentation of Greg's idea:
Code:
Sub ScratchMacro()
'Coded by Gregory K. Maxey
'https://www.msofficeforums.com/184580-post31.html

 Dim oRng As range
  Set oRng = ActiveDocument.range
  With oRng.Find
    .text = "<[0-9]{4,5}>"
    .MatchWildcards = True
    While .Execute
        If InStr(oRng.text, "2") > 0 Then MsgBox oRng.text
    Wend
  End With
lbl_Exit:
  Exit Sub
   End Sub
As I explained the number DOES NOT HAVE TO be a word, it can be part of a word.

If the data is:
a3452b
13452
then the code with .text = "[0-9]{4,5}" will find both 3452 and 13452. Meanwhile, the result 13452 is not good because 2 appears in the fifth place and not in 1-4
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:25 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