How to Extract a Code from Text Using REGEXEXTRACT and XLOOKUP in Excel
Question details
The user wants to extract a specific code pattern from a text string and use it as a lookup value to find related data in another range.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Cleaning and analyzing data where specific identifier codes are embedded within longer text strings.
- Observed behavior
- The goal is to accurately isolate the code using regular expressions and pass it to a lookup function to retrieve corresponding information.
Ensure your version of Excel supports the newly introduced REGEXEXTRACT function (currently available in Microsoft 365 Beta/Insider channels) and the XLOOKUP function.
Extract the Code Using REGEXEXTRACT
Use a regular expression pattern to isolate the specific code from the surrounding text.
If you only need to pull the code out of a text string without performing any lookup, you can use the REGEXEXTRACT function on its own. This is ideal for cleaning up messy data columns.
Click on the empty cell where you want the extracted code to appear.
Input the formula: =REGEXEXTRACT(A1,"(?i)\bY\d-\d{2}-[A-Z]{2}-\d{3}\b") assuming your text is in cell A1.
Press Enter. The formula will extract the matching code from the string.
Combine REGEXEXTRACT and XLOOKUP for Data Retrieval
Nest the REGEXEXTRACT formula inside XLOOKUP to extract the code and immediately find its related value in another table.
Use WPS Spreadsheet for Advanced Formula Processing
WPS Office provides a powerful, free Spreadsheet tool that supports a vast array of advanced lookup functions, ensuring seamless compatibility with your Excel files and data analysis workflows.
- 1. Open your workbook in WPS: Launch WPS Spreadsheet and open your existing .xlsx file containing the text data.
- 2. Define your data ranges: Ensure your target lookup data ranges are properly defined and organized.
- 3. Apply lookup formulas: Enter your preferred text manipulation and lookup formulas to retrieve the desired data seamlessly.
- 4. Save your progress: Save your file in .xlsx format to retain full compatibility across other spreadsheet applications.

Frequently Asked Questions
Why is my REGEXEXTRACT formula returning a #NAME? error?
The #NAME? error typically occurs if your version of Excel does not yet support the REGEXEXTRACT function. It is currently rolling out to specific Microsoft 365 Beta or Insider channels.
Can I use XLOOKUP with partial text matches without Regex?
Yes, if you only need a simple partial match, you can use wildcard characters (* or ?) inside XLOOKUP. However, Regex provides much more precise control over complex text patterns.
What does the "\b" mean in the regex formula?
The "\b" represents a "word boundary". It ensures that the formula only matches the exact standalone code pattern and prevents accidental extraction from a longer string of combined characters.





