Extract all 5 character alphanumerics in a string that where the 1st char is a letter, and the 2nd char is a number

andrewmshoe

New Member
Joined
Jun 24, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I found a few related questions, but waasn't sure how to modify the script for my purposes.
E.g: Extract multiple 5-digit numbers from alphanumeric text

StringDesired Output
Hello there P0D1C, im great,% U1212P0D1C U1212
My name is P6717 and I happyP6717

They will always start with a P or U. Also, there may be long strings where 10 or more of these alphanumerics need to get extracted.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
vba, data in col.A
Code:
Sub test()
    Dim r As Range, m, s$
    With CreateObject("VbScript.RegExp")
        .Global = True
        .Pattern = "\b[PU]\d[A-Za-z\d]{3}\b"
        For Each r In Range("a2", Range("a" & Rows.Count).End(xlUp))
            For Each m In .Execute(r)
                s = Trim(s & " " & m)
            Next
            r(, 2) = s: s = ""
        Next
    End With
End Sub
 
Upvote 1
Solution
Thank you, works great! I realized some of the 5 char start with lowercase so I made a small adjustment:

Rich (BB code):
Sub test()
    Dim r As Range, m, s$
    With CreateObject("VbScript.RegExp")
        .Global = True
        .Pattern = "\b[PUpu]\d[A-Za-z\d]{3}\b"
        For Each r In Range("a2", Range("a" & Rows.Count).End(xlUp))
            For Each m In .Execute(r)
                s = Trim(s & " " & m)
            Next
            r(, 2) = s: s = ""
        Next
    End With
End Sub
 
Last edited by a moderator:
Upvote 0
Looks like it didn't like that I tried to bold the "pu" I added... Anyway, thank you so much!
 
Upvote 0
Post few string that the code does not extract.

I believe your string is something different from what you posted in your initial post.
 
Upvote 0
Looks like it didn't like that I tried to bold the "pu" I added... Anyway, thank you so much!
You cannot use Formatting options within VBA code tags, but you can within Rich code tags.
I changed the code in your post to use Rich code tags.
 
Upvote 1
Post few string that the code does not extract.

I believe your string is something different from what you posted in your initial post.
u1343
p14rc

I was mistaken in saying they all start with P or U. There were also some the start with lowercase p or u.
 
Upvote 0
You could use the 'IgnoreCase' option:
VBA Code:
Function GetFive(str As String)
  Dim regEx As Object, rm As Object, m As Object, r As String
 
  Set regEx = CreateObject("VBScript.RegExp")
  With regEx
    .Global = True
    .IgnoreCase = True
    .Pattern = "[pu]\d[a-z\d]{3}"
    Set rm = .Execute(str)
  End With
  For Each m In rm: r = r & m.Value & " ": Next m
  GetFive = Application.Trim(r)
End Function
 
Upvote 1
No, what I wanted to see is whole string.
try remove \b from the pattern
Code:
       .Pattern = "[PUpu]\d[A-Za-z\d]{3}"
 
Upvote 0
Try this for formula option.
Book1
AB
1Hello there P0D1C, im great,% U1212P0D1C U1212
2My name is P6717 and I happyP6717
3This has both upper P6231 and lower p12312, plus U123Ud, minus u543d0dP6231 p12312 U123Ud u543d0d
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=LET(t,TEXTSPLIT(A1,{" ",","},,1),x,LEFT(t,1),TEXTJOIN(" ",,FILTER(t,((x="P")+(x="U"))*(ISNUMBER(--MID(t,2,1))))))
 
Last edited:
Upvote 1

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top