Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2025, 11:53 PM
ayomama ayomama is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find Office 2021
Novice
Using Regular Expression in Find
 
Join Date: Jul 2025
Posts: 5
ayomama is on a distinguished road
Default Using Regular Expression in Find

Hi


  • What is the flavor of Regular Expressions that Range.Find uses/understands?
  • What is the pattern (i.e. the value of strSearch below) that will fully match @PART_01, @ERTDRETHH_56 and @ASFGHFGHHFDERTGE_83?
Code:
Dim rngStory As Word.Range, strSearch As String


With rngStory.Find
    .MatchWildcards = True
    .text = strSearch
    .Wrap = WdFindWrap.wdFindStop
    .Execute Forward:=True
End With
Thanks!
Reply With Quote
  #2  
Old 07-22-2025, 01:53 AM
macropod's Avatar
macropod macropod is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could use a wildcard Find expression like:
strSearch = "\@[A-Z]@_[0-9]{2}"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 07-23-2025, 03:30 AM
ayomama ayomama is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find Office 2021
Novice
Using Regular Expression in Find
 
Join Date: Jul 2025
Posts: 5
ayomama is on a distinguished road
Default

Thank you Paul, works fine.


Now, what is the pattern to fully match @PART_01 and @ERTDRETHH ?


In other words, I'm looking for the syntax to make the last 3 characters "_01" optional. The 3rd character from last (if present) is always an underscore and the next 2 are numbers.


Thanks again.
Reply With Quote
  #4  
Old 07-23-2025, 08:55 AM
vivka vivka is offline Using Regular Expression in Find Windows 7 64bit Using Regular Expression in Find Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Hi! Unfortunately, you can't use a one-line regex to do this. Try the following, which will work on the selecton, not on the entire doc (it's my personal preference):
Code:
Sub Find_N_Embolden()

Dim rng As range
Dim FindTxt As String
 
    Application.ScreenUpdating = False
    Set rng = Selection.range
    
    FindTxt = "\@[A-Z]{1,}"
    Do
        With rng.Find
            .text = FindTxt
            .MatchWildcards = True
            .Forward = True
            .Wrap = wdFindStop
            .Execute
        End With
        If rng.Find.found And rng.InRange(Selection.range) Then
            If rng.Characters.Last.Next = "_" And _
                rng.Characters.Last.Next.Next Like "[0-9]" And _
                rng.Characters.Last.Next.Next.Next Like "[0-9]" And _
                Not rng.Characters.Last.Next.Next.Next.Next Like "[A-Za-z0-9]" Then
                rng.MoveEnd unit:=wdCharacter, count:=3
                rng.Font.Bold = True: GoTo lbl_Next
            ElseIf rng.Characters.Last.Next <> "_" Then
                rng.Font.Bold = True
            Else: GoTo lbl_Next
            End If
        Else
            Exit Do
        End If
lbl_Next:
        rng.Collapse wdCollapseEnd
     Loop
Application.ScreenUpdating = True
Set rng = Nothing
End Sub

Last edited by vivka; 07-23-2025 at 10:12 PM.
Reply With Quote
  #5  
Old 07-23-2025, 02:15 PM
macropod's Avatar
macropod macropod is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ayomama View Post
Now, what is the pattern to fully match @PART_01 and @ERTDRETHH ?


In other words, I'm looking for the syntax to make the last 3 characters "_01" optional. The 3rd character from last (if present) is always an underscore and the next 2 are numbers.
You could use a wildcard Find expression like:
strSearch = "\@[A-Z]{2}[A-Z_0-9]{1,}"
but you may get false matches
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 07-24-2025, 03:21 PM
ayomama ayomama is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find Office 2021
Novice
Using Regular Expression in Find
 
Join Date: Jul 2025
Posts: 5
ayomama is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Hi! Unfortunately, you can't use a one-line regex to do this. Try the following, which will work on the selecton, not on the entire doc (it's my personal preference):
Code:
Sub Find_N_Embolden()
...
End Sub
Thank you for this. Not tested yet but this workaround makes sense given the limitations of Word's regex.
Reply With Quote
  #7  
Old 07-25-2025, 08:31 AM
vivka vivka is offline Using Regular Expression in Find Windows 7 64bit Using Regular Expression in Find Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

You are welocme! The code seems to work correctly.
Reply With Quote
  #8  
Old 07-30-2025, 04:33 PM
gmaxey gmaxey is offline Using Regular Expression in Find Windows 10 Using Regular Expression in Find 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

Vivka,


Question and comment about your code below.


1. I notice that you (and others I have observed) use the Find.Found method. E.g. If rng.find.found ...


Why? find.found returns True but so does


If .Execute.


2. Your code will error if the one of the find returns is at the end of the document.


Consider:


Code:
Sub Find_N_Embolden()
'Revised GKM
Dim oRng As Range
  Application.ScreenUpdating = False
  Set oRng = Selection.Range
  With oRng.Find
    .Text = "\@[A-Z]{1,}"
    .MatchWildcards = True
    .Forward = True
    .Wrap = wdFindStop
    Do While .Execute
      If oRng.InRange(Selection.Range) Then
        Select Case True
          Case oRng.End = ActiveDocument.Range.End - 1
            oRng.Font.Bold = True
          Case oRng.Characters.Last.Next <> "_"
            oRng.Font.Bold = True
          Case oRng.End <= ActiveDocument.Range.End - 4
            oRng.MoveEnd unit:=wdCharacter, Count:=3
            If Right(oRng.Text, 3) Like "_[0-9][0-9]" Then oRng.Font.Bold = True
        End Select
      End If
      oRng.Collapse wdCollapseEnd
    Loop
  End With
  Application.ScreenUpdating = True
  Set oRng = Nothing
End Sub

Quote:
Originally Posted by vivka View Post
Hi! Unfortunately, you can't use a one-line regex to do this. Try the following, which will work on the selecton, not on the entire doc (it's my personal preference):
Code:
Sub Find_N_Embolden()

Dim rng As range
Dim FindTxt As String
 
    Application.ScreenUpdating = False
    Set rng = Selection.range
    
    FindTxt = "\@[A-Z]{1,}"
    Do
        With rng.Find
            .text = FindTxt
            .MatchWildcards = True
            .Forward = True
            .Wrap = wdFindStop
            .Execute
        End With
        If rng.Find.found And rng.InRange(Selection.range) Then
            If rng.Characters.Last.Next = "_" And _
                rng.Characters.Last.Next.Next Like "[0-9]" And _
                rng.Characters.Last.Next.Next.Next Like "[0-9]" And _
                Not rng.Characters.Last.Next.Next.Next.Next Like "[A-Za-z0-9]" Then
                rng.MoveEnd unit:=wdCharacter, count:=3
                rng.Font.Bold = True: GoTo lbl_Next
            ElseIf rng.Characters.Last.Next <> "_" Then
                rng.Font.Bold = True
            Else: GoTo lbl_Next
            End If
        Else
            Exit Do
        End If
lbl_Next:
        rng.Collapse wdCollapseEnd
     Loop
Application.ScreenUpdating = True
Set rng = Nothing
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 07-30-2025, 10:37 PM
vivka vivka is offline Using Regular Expression in Find Windows 7 64bit Using Regular Expression in Find Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Thank you, Greg, for your remarks! I fully accept them. My final code taking advantage of your suggestions (If conditions can be replaced wih Case conditions):
Code:
Sub Find_N_Embolden()
'In selection, embolden all '@AnyNumberOfCapitals' or
''@AnyNumberOfCapitals_DigitDigitNotFollowedByDigitOrLetter'.

Dim oRng As range
Dim FindTxt As String
 
    Application.ScreenUpdating = False
    Set oRng = Selection.range
    
    FindTxt = "\@[A-Z]{1,}"
    Do
        With oRng.Find
            .text = FindTxt
            .MatchWildcards = True
            .Forward = True
            .Wrap = wdFindStop
            If .Execute And oRng.InRange(Selection.range) Then
                If oRng.End >= ActiveDocument.range.End - 4 Then
                    oRng.Font.Bold = True: GoTo lbl_Next
                ElseIf oRng.Characters.Last.Next <> "_" Then
                    oRng.Font.Bold = True: GoTo lbl_Next
                ElseIf Right(ActiveDocument.range(oRng.start, oRng.End + 4), 4) Like "_[0-9][0-9][!A-Za-z0-9]" Then
                    oRng.MoveEnd unit:=wdCharacter, count:=3
                    oRng.Font.Bold = True: GoTo lbl_Next
                Else: GoTo lbl_Next
                End If
            Else
                Exit Do
            End If
        End With
lbl_Next:
        oRng.Collapse wdCollapseEnd
     Loop
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub

Last edited by vivka; 07-31-2025 at 06:59 AM.
Reply With Quote
Reply

Tags
regex, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Regular Expression in Find Regular expression confusion! VBAFiddler Word VBA 11 02-13-2021 01:16 AM
Using Regular Expression in Find find/replace wildcard expression help LQuinn Word VBA 2 01-28-2021 11:57 PM
Is it possible to use wild cards or regular expression in autocorrect kcvinu Word 2 12-14-2018 02:06 PM
Using Regular Expression in Find Regular Expression Syntax to Search for Alternative Words qubie Word 2 11-29-2017 05:48 AM
VBA code to enable Regular Expression ronmayerle Word VBA 2 11-20-2014 01:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:39 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