{"id":2946,"date":"2024-03-22T07:00:48","date_gmt":"2024-03-22T14:00:48","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=2946"},"modified":"2024-04-03T03:44:56","modified_gmt":"2024-04-03T10:44:56","slug":"introducing-regular-expression-regex-support-in-azure-sql-db","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/introducing-regular-expression-regex-support-in-azure-sql-db\/","title":{"rendered":"Introducing Regular Expression (Regex) Support in Azure SQL DB"},"content":{"rendered":"<p>We are pleased to announce the private preview of regular expressions (regex) support in Azure SQL Database. Regex is a powerful tool that allows you to search, manipulate, and validate text data in flexible ways. With regex support, you can enhance your SQL queries with pattern matching, extraction, replacement, and more. You can also combine them with other SQL functions and operators to create complex expressions and logic.<\/p>\n<p>This feature can help you to:<\/p>\n<ul>\n<li>Enhance your data quality and accuracy by validating and correcting data formats, such as phone numbers, email addresses, zip codes, etc.<\/li>\n<li>Extract valuable insights and information from the data by extracting and grouping specific text patterns, such as keywords, hashtags, mentions, etc.<\/li>\n<li>Transform and standardize your data by replacing, splitting, or joining text patterns, such as abbreviations, acronyms, synonyms, etc.<\/li>\n<li>Clean and optimize your data by removing or filtering out unwanted text patterns, such as whitespace, punctuation, duplicates, etc.<\/li>\n<\/ul>\n<p>The Regex feature in Azure SQL DB follows the <strong>POSIX<\/strong> standard and is compatible with the standard regex syntax and supports a variety of regex functions, such as <strong>REGEXP_LIKE, REGEXP_COUNT, REGEXP_INSTR, REGEXP_REPLACE, <\/strong>and <strong>REGEXP_SUBSTR<\/strong>. The feature also supports case sensitivity, character classes, quantifiers, anchors, and capturing groups. The feature is available for all Azure SQL DB service tiers and offerings.<\/p>\n<h2>Feature Exploration<\/h2>\n<p>Here is the list of Regex functions that are being introduced:<\/p>\n<ul>\n<li>REGEXP_LIKE: This function returns TRUE if a string matches a regular expression pattern, or FALSE otherwise.<\/li>\n<li>REGEXP_COUNT: This function returns the number of times a regular expression pattern matches in a string.<\/li>\n<li>REGEXP_INSTR: This function returns the starting or ending position, based on the specified option, of the given occurrence of a regular expression pattern in a string.<\/li>\n<li>REGEXP_REPLACE: This function returns a modified string replaced by a \u2018replacement string\u2019, where occurrence of the regular expression pattern found.<\/li>\n<li>REGEXP_SUBSTR: This function returns a substring that matches a regular expression pattern from a string.<\/li>\n<\/ul>\n<h2>Examples<\/h2>\n<p>We\u2019ll use the following table as an example to demonstrate the usage of these functions. The table contains some information about employees, such as their names, email addresses, and phone numbers.<\/p>\n<table style=\"border-collapse: collapse\">\n<tbody>\n<tr>\n<td><strong>ID\u00a0<\/strong><\/td>\n<td><strong>Name\u00a0<\/strong><\/td>\n<td><strong>Email\u00a0<\/strong><\/td>\n<td><strong>Phone_Number\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td>101<\/td>\n<td>John Doe<\/td>\n<td>john@contoso.com<\/td>\n<td>123-456-7890<\/td>\n<\/tr>\n<tr>\n<td>102<\/td>\n<td>Alice Smith<\/td>\n<td>alice@fabrikam.com<\/td>\n<td>234-567-8901<\/td>\n<\/tr>\n<tr>\n<td>103<\/td>\n<td>Bob Johnson<\/td>\n<td>bob@fabrikam.net<\/td>\n<td>345-678-9012<\/td>\n<\/tr>\n<tr>\n<td>104<\/td>\n<td>Eve Jones<\/td>\n<td>eve@contoso.com<\/td>\n<td>456-789-0123<\/td>\n<\/tr>\n<tr>\n<td>105<\/td>\n<td>Charlie Brown<\/td>\n<td>charlie@contoso.co.in<\/td>\n<td>567-890-1234<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The following are some examples of using Regex functions in SQL queries.<\/p>\n<h3>REGEXP_LIKE<\/h3>\n<p>This function returns True if the input string matches the regex pattern, and False otherwise. You can use it to filter rows based on a regex condition and to apply check constraints to ensure that the data fulfils the specified criteria.<\/p>\n<p>Let\u2019s create Employee table with some records and Check constraints for <span style=\"font-family: 'andale mono', monospace\">Email<\/span> and <span style=\"font-family: 'andale mono', monospace\">Phone_Number<\/span> columns:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- Create Employees table with some records and check constraints for Email and Phone_Number columns\r\nDROP TABLE IF EXISTS Employees\r\nCREATE TABLE Employees (\r\n    ID INT IDENTITY(101,1),\r\n    [Name] VARCHAR(150),\r\n    Email VARCHAR(320)\r\n    CHECK (REGEXP_LIKE(Email, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$')),\r\n    Phone_Number VARCHAR(20)\r\n    CHECK (REGEXP_LIKE (Phone_Number, '^(\\d{3})-(\\d{3})-(\\d{4})$'))\r\n);\r\n-- Insert some sample data\r\nINSERT INTO Employees ([Name], Email, Phone_Number) VALUES\r\n    ('John Doe', 'john@contoso.com', '123-456-7890'),\r\n    ('Alice Smith', 'alice@fabrikam.com', '234-567-8901'),\r\n    ('Bob Johnson', 'bob@fabrikam.net','345-678-9012'),\r\n    ('Eve Jones', 'eve@contoso.com', '456-789-0123'),\r\n    ('Charlie Brown', 'charlie@contoso.co.in', '567-890-1234');<\/code><\/pre>\n<p>Insert a new row into the Employees table with the following values to validate the Check constraints:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">--Failure:  \r\nINSERT INTO Employees ([Name], Email, Phone_Number) VALUES \r\n    ('Demo Data', 'demo@contoso@com', '123-456_7890') \r\n\r\n--Success: \r\nINSERT INTO Employees ([Name], Email, Phone_Number) VALUES \r\n    ('Demo Data', 'demo@contoso.com', '123-456-7890') <\/code><\/pre>\n<p>Filter rows based on regex condition:<\/p>\n<p>For example, the following query returns all the rows from the Employees table where the email address domain ends with &#8216;.com&#8217;.<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- find all the employees whose email addresses end with .com\r\nSELECT [Name], Email\r\nFROM Employees\r\nWHERE REGEXP_LIKE(Email, '\\.com$');<\/code><\/pre>\n<p>Results:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Name<\/td>\n<td>Email<\/td>\n<\/tr>\n<tr>\n<td>John Doe<\/td>\n<td>john@contoso.com<\/td>\n<\/tr>\n<tr>\n<td>Alice Smith<\/td>\n<td>alice@fabrikam.com<\/td>\n<\/tr>\n<tr>\n<td>Eve Jones<\/td>\n<td>eve@contoso.com<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>REGEXP_COUNT<\/h3>\n<p>This function returns the number of times a regex pattern matches in the input string. You can use it to count the occurrences of a pattern within a string. For example, the following query returns the number of vowels in each Employee\u2019s name.<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- for each employee, count the number of vowels in their name\r\nSELECT [Name], REGEXP_COUNT([Name], '[AEIOU]',1,'i') AS Vowel_Count\r\nFROM Employees;<\/code><\/pre>\n<p>Results:<\/p>\n<table width=\"0\">\n<tbody>\n<tr>\n<td width=\"113\"><strong>Name\u00a0<\/strong><\/td>\n<td width=\"213\"><strong>Vowel_Count\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"113\">John Doe<\/td>\n<td width=\"213\">3<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Alice Smith<\/td>\n<td width=\"213\">4<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Bob Johnson<\/td>\n<td width=\"213\">3<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Eve Jones<\/td>\n<td width=\"213\">4<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Charlie Brown<\/td>\n<td width=\"213\">4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>REGEXP_INSTR<\/h3>\n<p>This function returns the starting or ending position, based on the specified option, of the given occurrence of a regular expression pattern in a string.<span data-ccp-props=\"{}\"> You can use it to locate the index of a pattern within a string.<\/span>\u00a0For example, the following query returns the position of &#8216;@&#8217; in each Employee&#8217;s email address.<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- for each employee, show their name, email, and the position of the @ sign in their email\r\nSELECT [Name], Email, REGEXP_INSTR(email, '@') AS Position_of_@\r\nFROM Employees;<\/code><\/pre>\n<p>Results:<\/p>\n<table width=\"0\">\n<tbody>\n<tr>\n<td width=\"113\"><strong>Name\u00a0<\/strong><\/td>\n<td width=\"213\"><strong>Email\u00a0<\/strong><\/td>\n<td width=\"124\"><strong>Position_of_@\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"113\">John Doe<\/td>\n<td width=\"213\">john@contoso.com<\/td>\n<td width=\"124\">5<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Alice Smith<\/td>\n<td width=\"213\">alice@fabrikam.com<\/td>\n<td width=\"124\">6<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Bob Johnson<\/td>\n<td width=\"213\">bob@fabrikam.net<\/td>\n<td width=\"124\">4<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Eve Jones<\/td>\n<td width=\"213\">eve@contoso.com<\/td>\n<td width=\"124\">4<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Charlie Brown<\/td>\n<td width=\"213\">charlie@contoso.co.in<\/td>\n<td width=\"124\">8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>REGEXP_REPLACE<\/h3>\n<p>This function returns a modified string replaced by a \u2018replacement string\u2019, where occurrence of the regular expression pattern found.<span data-ccp-props=\"{}\">\u00a0<\/span>You can use it to modify or transform text data based on a regex pattern. For example, the following query returns the phone number of each Employee in a standardized format.<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- format the phone numbers in the Employees table to the format (XXX) XXX-XXXX.\r\nSELECT Phone_Number, REGEXP_REPLACE(Phone_Number, '(\\d{3})-(\\d{3})-(\\d{4})', '(\\1) \\2-\\3',1) AS Phone_Format\r\nFROM Employees;<\/code><\/pre>\n<p>Results:<\/p>\n<table width=\"0\">\n<tbody>\n<tr>\n<td width=\"311\"><strong>EMP_PHONE\u00a0<\/strong><\/td>\n<td width=\"311\"><strong>PHONE_FORMAT\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"311\">123-456-7890<\/td>\n<td width=\"311\">(123) 456-7890<\/td>\n<\/tr>\n<tr>\n<td width=\"311\">234-567-8901<\/td>\n<td width=\"311\">(234) 567-8901<\/td>\n<\/tr>\n<tr>\n<td width=\"311\">345-678-9012<\/td>\n<td width=\"311\">(345) 678-9012<\/td>\n<\/tr>\n<tr>\n<td width=\"311\">456-789-0123<\/td>\n<td width=\"311\">(456) 789-0123<\/td>\n<\/tr>\n<tr>\n<td width=\"311\">567-890-1234<\/td>\n<td width=\"311\">(567) 890-1234<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>REGEXP_SUBSTR<\/h3>\n<p>This function returns the substring that matches the regular expression pattern from a string. You can use it to extract parts of a string based on a regex pattern. For example, the following query returns the domain name of each employee&#8217;s email address.<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">-- for each employee, show the domain of their email address\r\nSELECT [Name], Email, REGEXP_SUBSTR(email, '@(.+)$', 1, 1,'c',1) AS Domain\r\nFROM Employees;<\/code><\/pre>\n<p>Results:<\/p>\n<table width=\"0\">\n<tbody>\n<tr>\n<td width=\"113\"><strong>Name\u00a0<\/strong><\/td>\n<td width=\"213\"><strong>Email\u00a0<\/strong><\/td>\n<td width=\"124\"><strong>Domain\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"113\">John Doe<\/td>\n<td width=\"213\">john@contoso.com<\/td>\n<td width=\"124\">contoso.com<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Alice Smith<\/td>\n<td width=\"213\">alice@fabrikam.com<\/td>\n<td width=\"124\">fabrikam.com<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Bob Johnson<\/td>\n<td width=\"213\">bob@fabrikam.net<\/td>\n<td width=\"124\">fabrikam.net<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Eve Jones<\/td>\n<td width=\"213\">eve@contoso.com<\/td>\n<td width=\"124\">contoso.com<\/td>\n<\/tr>\n<tr>\n<td width=\"113\">Charlie Brown<\/td>\n<td width=\"213\">charlie@contoso.co.in<\/td>\n<td width=\"124\">contoso.co.in<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>Private Preview Sign-up<\/h2>\n<p>We are currently accepting requests from customers who would like to participate in the private preview and try out the regex feature. If you are interested, please fill out <a href=\"https:\/\/aka.ms\/regex-preview-signup\">this form<\/a>: <a href=\"https:\/\/aka.ms\/regex-preview-signup\">https:\/\/aka.ms\/regex-preview-signup<\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up.png\"><img decoding=\"async\" class=\"wp-image-3081 size-medium aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-300x300.png\" alt=\"Image NewQRCode for Regex in Azure SQL Database Private Preview Sign up\" width=\"300\" height=\"300\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-300x300.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-1024x1024.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-150x150.png 150w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-768x768.png 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-1536x1536.png 1536w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-24x24.png 24w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-48x48.png 48w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up-96x96.png 96w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2024\/03\/NewQRCode-for-Regex-in-Azure-SQL-Database-Private-Preview-Sign-up.png 1890w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Wrapping up<\/h2>\n<p>We value your feedback and suggestions as we continue to improve and enhance SQL DB. Please let us know what you think of the regex feature and how it helps you with your data analysis and manipulation. You can submit your feedback using <a href=\"https:\/\/aka.ms\/sqldbregex-feedback\">this Form<\/a> or leave a comment on this blog.<\/p>\n<p>Thank you for choosing Azure SQL Database as your data platform. We hope you enjoy the regex feature and find it beneficial for your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are pleased to announce the private preview of regular expressions (regex) support in Azure SQL Database. Regex is a powerful tool that allows you to search, manipulate, and validate text data in flexible ways. With regex support, you can enhance your SQL queries with pattern matching, extraction, replacement, and more. You can also combine [&hellip;]<\/p>\n","protected":false},"author":29335,"featured_media":81,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[607,606,608],"class_list":["post-2946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","tag-regex","tag-regular-expressions","tag-sql-regex-functions"],"acf":[],"blog_post_summary":"<p>We are pleased to announce the private preview of regular expressions (regex) support in Azure SQL Database. Regex is a powerful tool that allows you to search, manipulate, and validate text data in flexible ways. With regex support, you can enhance your SQL queries with pattern matching, extraction, replacement, and more. You can also combine [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/29335"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=2946"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2946\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=2946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=2946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=2946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}