Thread: [Solved] Regex-pattern
View Single Post
 
Old 01-06-2025, 01:15 PM
batman1 batman1 is offline Windows 11 Office 2013
Advanced Beginner
 
Join Date: Jan 2025
Posts: 57
batman1 is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
In the code
Code:
<[0-9]{4,5}>
< and > identify a 4- or 5-digit 'word' without any letters. You can test it. As for finding 2 in all positions but fifth, Greg's 2nd condition can be used. So, the final code is:
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(left(oRng.text, 4), "2") > 0 Then MsgBox oRng.text     
     Wend   
   End With 
lbl_Exit:   
   Exit Sub    
 End Sub

I have already explained that the task does not require the number to be a word - it can be part of a word.

With data:
13452234567
a3452b
67892
123456 23556789 3456789 a234667 13452234567 a3452b 1342 222

podany kod znajdzie tylko 1342 . Tymczasem wyniki =
34522
3452
7892
12345
23556
23466
34522
3452
1342