2

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

That worked a treat. Exactly what i need! Thank you very much for the timely response .
Is there anyway to make the \d{2} variable? Sometimes it might be a total of 3 characters or 4. It needs to check that after the first 2 characters "Is there another alpha char?" if there is take that, if there isn't and it's a number, stop.
Ahh, disregard last comment, it does indeed do that already. Perfect!
@MichaelLivingston FYI: \d{2,4} will match 2, 3 or 4 digits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.