Reputation: 35
I am trying to write some regex that will pull 2 - 3 alpha charters from a string. This is an example of a string "01EY003 - P3 MOTOR STOP". The "E" & "Y" is what i need from this specific string. The characters are always preceded by 2 numeric characters. Where the "Y" is, that can be up to 3 characters. The desired characters could also be in any position in the string. So the string could be something like "P3 MOTOR STOP, 01EYAQ003".
Here is some sample data. Some examples of source strings on the left, and what i want to extract on the right.
"01FT108 - TMPRD OIL FLOW E944A" - FT
"01FIT110 - FLUSH FROM E101 SHELL" - FIT
"01FC111 - CUTTER FR P21/22 FLOW" - FC
"01FC112 - P6A E946 HEADER FLOW " - FC
"01FT113 - TMPRD OIL FLOW E946A" - FT
"5 TAR LINE FLOW- 01FT005" - FT
Any help is appreciated. I have been scouring forums trying to learn regex, as I've never had to use it much, so i am pretty awful at it.
I am embedding this regex within vb script. So i have tried using some simple regex "[a-zA-Z]". Then using the code to calculate, as i am pretty bad with reg ex.
This is where i will be using the regex. I am good with the vbscript, this was just a little test.
Sub Test_Execute(Batch)
Dim objRE, objMatch, i
Stop
Set objRE = New RegExp
objRE.Pattern = "[a-zA-Z]"
objRE.Global = True
objRE.IgnoreCase = True
Set objMatch = objRE.Execute(Document.DwgTitleLine2)
For i = 0 To 5
If objRE.Test(objMatch(i).value)Then
WinMsgBox "First Char True"
Else
WinMsgBox "First Char False"
End If
Next
End Sub
When i used this code, with my simple regex. Every loop was bringing back only Alpha characters. Even everything within "P3 MOTOR STOP", which i don't care about. Obviously because i have virtually no rules in the regex expression.
Upvotes: 2
Views: 321
Reputation: 627507
You may use
(?:^|\s)\d{2}([a-zA-Z]+)
See the regex demo
It matches:
(?:^|\s)
- start of string or whitespace\d{2}
- two digits([a-zA-Z]+)
- Group 1 (match.Submatches(0)
): 1+ ASCII letters.VBA:
Set objMatch = objRE.Execute(Document.DwgTitleLine2)
For Each m In objMatch
Debug.Print m.Submatches(0)
Next
Upvotes: 1