Thread: [Solved] Regex-pattern
View Single Post
 
Old 01-06-2025, 09:27 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

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/