Regex in Fabric Warehouse: clean messy text in T-SQL

The five scalar regular expression functions in Fabric Data Warehouse and the SQL analytics endpoint (preview), REGEXP_LIKE, REGEXP_SUBSTR, REGEXP_REPLACE, REGEXP_COUNT and REGEXP_INSTR. What each returns, a worked clean-up of a messy product feed whose counts add up, flags, the limits (2 MB input, no table-valued functions, no collation), and where the same functions work outside Fabric.

Short answer: Fabric Data Warehouse and the SQL analytics endpoint support five scalar regex functions in preview: REGEXP_LIKE (does it match? use in WHERE or CASE), REGEXP_SUBSTR (pull out the match or a capture group), REGEXP_REPLACE (replace or reformat), REGEXP_COUNT (how many matches) and REGEXP_INSTR (where the match starts or ends). Patterns follow the RE2 syntax, flags i, c, m and s control matching, and input can be up to 2 MB. The table-valued REGEXP_MATCHES and REGEXP_SPLIT_TO_TABLE are not available in the warehouse. The same functions work in Azure SQL Database, SQL database in Fabric and SQL Server 2025.

Every warehouse has a column like this: a free-text field from an old system where the product code sits somewhere between words, sometimes in lower case, sometimes twice, sometimes missing a digit. Before, cleaning it meant PATINDEX, SUBSTRING and CHARINDEX stacked five deep, or a trip to a notebook. Now one function call does it in the query that loads the table.

What does each function return?

FunctionReturnsTypical job
REGEXP_LIKEtrue or falsefilter valid values; wrap in CASE to show as a column
REGEXP_SUBSTRthe nth match, or one capture groupextract a code, a domain, a number
REGEXP_REPLACEthe text with matches replacedcollapse spaces, reformat, mask
REGEXP_COUNTnumber of matchesfind rows holding more than one code
REGEXP_INSTRstart or end position of a matchsplit text around a match
The five regex functions in Fabric Data Warehouse

All five return one value per row, so they work in SELECT, WHERE, JOIN, GROUP BY, ORDER BY and CASE.

How do you clean a messy column?

A product feed where codes should look like AB- plus four digits:

SELECT
    Id,
    UPPER(REGEXP_SUBSTR(Raw, 'AB-\d{4}', 1, 1, 'i'))  AS FirstCode,
    REGEXP_COUNT(Raw, 'AB-\d{4}', 1, 'i')             AS CodeCount,
    TRIM(REGEXP_REPLACE(Raw, '\s+', ' '))             AS CleanText,
    CASE WHEN REGEXP_LIKE(Raw, 'AB-\d{4}', 'i')
         THEN 'ok' ELSE 'check' END                   AS Status
FROM (VALUES
    (1, 'SKU: ab-1042 / blue'),
    (2, 'sku AB-2210 red'),
    (3, 'no code'),
    (4, '  AB-0007   green'),
    (5, 'AB-77'),
    (6, 'ref ab-3301, ab-3302')
) AS feed (Id, Raw);

The 'i' flag makes ab-1042 match the upper-case pattern; UPPER then normalises what was found. \d{4} insists on exactly four digits, so AB-77 does not count.

Do the results add up?

IdFirstCodeCodeCountStatus
1AB-10421ok
2AB-22101ok
30check
4AB-00071ok
50check
6AB-33012ok
Total4 rows with a code5 codes2 rows to check
What the query returns

The counts: 1 + 1 + 0 + 1 + 0 + 2 = 5 codes in 4 of the 6 rows, and the 2 rows marked "check" are exactly those with a count of 0. Row 6 shows why REGEXP_COUNT matters: FirstCode keeps only AB-3301, so a row with two codes needs a second REGEXP_SUBSTR(..., 1, 2, 'i') or a review.

  • Row 11
  • Row 21
  • Row 30
  • Row 41
  • Row 50
  • Row 62
Codes found per row

What else is it good for?

  • Extracting part of a match with a capture group, the sixth argument of REGEXP_SUBSTR: REGEXP_SUBSTR(Email, '@(.+)$', 1, 1, 'c', 1) returns the email domain.
  • Reformatting with back-references: REGEXP_REPLACE(Phone, '^(\d{3})-(\d{3})-(\d{4})$', '(\1) \2-\3').
  • Masking: keep the part you want as a capture group and replace the rest, for example REGEXP_REPLACE(Card, '^\d{12}(\d{4})$', '************\1') shows only the last four digits of a 16-digit number.
  • Grouping by an extracted value, for example customers per email domain, since the functions are allowed in GROUP BY.

Which flags are there?

c case-sensitive (the default), i case-insensitive, m makes ^ and $ match at every line, s lets . match a newline. Flags combine in one string of up to 30 characters; with contradictory flags the last one wins, so 'ic' ends up case-sensitive.

What goes wrong?

  • Relying on the column's collation. Regex ignores collation. A case-insensitive column still matches case-sensitively unless you pass 'i'.
  • Returning REGEXP_LIKE as a column. It is a Boolean; wrap it in CASE.
  • Patterns copied from other tools. The functions follow RE2 syntax; a pattern written for another engine can use constructs RE2 does not accept. Test it on a few rows first.
  • Expecting a split into rows. REGEXP_SPLIT_TO_TABLE and REGEXP_MATCHES are not in the warehouse; use REGEXP_SUBSTR with an occurrence number, or STRING_SPLIT on a cleaned delimiter.
  • Huge values and patterns. Input up to 2 MB, patterns up to 8,000 bytes, and counted repeats up to 1,000.
  • Preview. Test the exact patterns you depend on before building a production load on them.

Where can you check this?

Where do you start?

Pick the free-text column your team cleans by hand most often, write one REGEXP_LIKE that marks the good rows, and count the rest. That count is your data quality number before and after. The SQL analytics endpoint runs the same functions on lakehouse tables, and window functions pair well with them for de-duplicating cleaned codes.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.