![]() |
|
#1
|
|||
|
|||
![]()
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ü\-]+)*, Code:
, [A-Z]([ A-Za-zü\-])*, P.S. Naturally, making "improvements" to the basic code (Batman's) is less demanding than making the basic code. |
#2
|
|||
|
|||
![]()
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"
|
#3
|
|||
|
|||
![]() Quote:
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 12345 – Match 56789 – No Match 123 – No Match 1234 – Match 4321 – Match 8945 – No Match 98765 – No Match 124567 – No match 54312 – No match |
#4
|
|||
|
|||
![]() Quote:
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 |
#5
|
|||
|
|||
![]()
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 Greg and Batman1, thank you for another good lesson! |
#6
|
|||
|
|||
![]() Quote:
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 |
#7
|
|||
|
|||
![]()
In the code
Code:
<[0-9]{4,5}> 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 |
#8
|
|||
|
|||
![]() Quote:
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 |
#9
|
|||
|
|||
![]()
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. |
#10
|
|||
|
|||
![]() Quote:
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," |
#11
|
|||
|
|||
![]()
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
|
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
alex100 | Word VBA | 1 | 01-02-2021 02:39 PM |
![]() |
totoMSOF | Word VBA | 19 | 03-11-2019 01:28 PM |
![]() |
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 |