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.
 
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))))))
This is really interesting and works for 95% of the dataset I have. My input data is quite messy so there are a number of edge cases, and I narrowed one down here to show a few issues:

...evehile.P0MC4 and P1EUABoth codes …P1EUABoth

This method would be preferred as I'd like to convert this all to DAX for PowerBI at some point and this would be a much simpler starting point.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If the delimiters around the code are limited to space, commas and periods, you can try this. You can add more delimiters to this part of the formula as needed
Rich (BB code):
=LET(t,TEXTSPLIT(A1,{" ",",","."},,1),x,LEFT(t,1),TEXTJOIN(" ",,FILTER(LEFT(t,5),((x="P")+(x="U"))*(ISNUMBER(--MID(t,2,1))))))
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 p1231 U123U u543d
4...evehile.P0MC4 and P1EUABoth codes …P0MC4 P1EUA
Sheet3
Cell Formulas
RangeFormula
B1:B4B1=LET(t,TEXTSPLIT(A1,{" ",",","."},,1),x,LEFT(t,1),TEXTJOIN(" ",,FILTER(LEFT(t,5),((x="P")+(x="U"))*(ISNUMBER(--MID(t,2,1))))))
 
Upvote 1
If the delimiters around the code are limited to space, commas and periods, you can try this. You can add more delimiters to this part of the formula as needed
Rich (BB code):
=LET(t,TEXTSPLIT(A1,{" ",",","."},,1),x,LEFT(t,1),TEXTJOIN(" ",,FILTER(LEFT(t,5),((x="P")+(x="U"))*(ISNUMBER(--MID(t,2,1))))))
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 p1231 U123U u543d
4...evehile.P0MC4 and P1EUABoth codes …P0MC4 P1EUA
Sheet3
Cell Formulas
RangeFormula
B1:B4B1=LET(t,TEXTSPLIT(A1,{" ",",","."},,1),x,LEFT(t,1),TEXTJOIN(" ",,FILTER(LEFT(t,5),((x="P")+(x="U"))*(ISNUMBER(--MID(t,2,1))))))
I can't thank you enough! I will check for other problematic delimiters and add as needed.
 
Upvote 0
Can I select more than 1 answer as solution? Also, I'd like to "buy a coffee" for you guys if that sort of thing is allowed/you have something set up for that.
 
Upvote 0
You're welcome.
You can see this thread for more details about marking a solution, but you can only choose one.
We're all volunteers here so no repayment is needed. A thank you is sufficient.
 
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