{"id":1219,"date":"2024-12-23T17:47:53","date_gmt":"2024-12-23T10:47:53","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1219"},"modified":"2025-01-03T14:28:18","modified_gmt":"2025-01-03T07:28:18","slug":"postgresql-regular-expressions","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/","title":{"rendered":"PostgreSQL Regular Expressions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL regular expressions to match text strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-posix-regular-expressions'>Introduction to POSIX regular expressions <a href=\"#introduction-to-posix-regular-expressions\" class=\"anchor\" id=\"introduction-to-posix-regular-expressions\" title=\"Anchor for Introduction to POSIX regular expressions\">#<\/a><\/h2>\n\n\n\n<p>A regular expression, regex, is a sequence of characters that defines a <em>search pattern<\/em>.<\/p>\n\n\n\n<p>For example, the <code>\\d+<\/code> regular expression matches one or more digits. They can be any digits, not specific to particular digits. The string <code>\"iPhone 15\"<\/code> will match the regular expression <code>\\d<\/code>.<\/p>\n\n\n\n<p>PostgreSQL supports POSIX regular expressions, a standardized set of regular expressions defined by POSIX (Portable Operating System Interface).<\/p>\n\n\n\n<p class=\"note\">The Perl Compatible Regular Expressions (PCRE) are another standard besides the POSIX regular expressions. PostgreSQL does not natively support PCRE regular expressions.<\/p>\n\n\n\n<p>Let&#8217;s understand how POSIX regular expressions work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='character-classes'>Character Classes <a href=\"#character-classes\" class=\"anchor\" id=\"character-classes\" title=\"Anchor for Character Classes\">#<\/a><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\\d<\/code> matches any digit. It is equivalent to <code>[0-9]<\/code><\/li>\n\n\n\n<li><code>\\s<\/code> matches any whitespace character.<\/li>\n\n\n\n<li><code>\\w<\/code> matches any word character, including letters, digits, and underscores. It is equivalent to <code>[a-zA-Z0-9_]<\/code>.<\/li>\n\n\n\n<li>The uppercase letter negates the meaning of the character classes:<\/li>\n\n\n\n<li><code>\\D<\/code> matches any non-digit character, equivalent to <code>[^0-9]<\/code>.<\/li>\n\n\n\n<li><code>\\S<\/code> matches any non-whitespace character.<\/li>\n\n\n\n<li><code>\\W<\/code> matches any non-word character.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='anchors'>Anchors <a href=\"#anchors\" class=\"anchor\" id=\"anchors\" title=\"Anchor for Anchors\">#<\/a><\/h3>\n\n\n\n<p>Anchors match a position instead of characters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>^<\/code> matches the beginning of a string.<\/li>\n\n\n\n<li><code>$<\/code> matches the end of a string.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='quantifiers'>Quantifiers <a href=\"#quantifiers\" class=\"anchor\" id=\"quantifiers\" title=\"Anchor for Quantifiers\">#<\/a><\/h3>\n\n\n\n<p>Quantifiers match the number of instances of a character set:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>*<\/code> matches zero or more.<\/li>\n\n\n\n<li><code>+<\/code> matches one or more.<\/li>\n\n\n\n<li><code>?<\/code> matches zero or one.<\/li>\n\n\n\n<li><code>{n}<\/code> matches exactly n times.<\/li>\n\n\n\n<li><code>{n,}<\/code> matches at least n times.<\/li>\n\n\n\n<li><code>{n,m}<\/code> matches a range from n to m times, where n &lt; m.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='sets-ranges'>Sets &amp; Ranges <a href=\"#sets-ranges\" class=\"anchor\" id=\"sets-ranges\" title=\"Anchor for Sets &amp; Ranges\">#<\/a><\/h3>\n\n\n\n<p>Use <code>[]<\/code> to create a set that matches any character in the set. For example, <code>[abc]<\/code> matches a, b, or c.<br>Use a hyphen <code>-<\/code> within the square brackets <code>[]<\/code> to create a range. For example, <code>[a-z]<\/code> is a range of characters from a to z. <code>[0-9]<\/code> is a range of digits from 0 to 9.<br>Use <code>^<\/code> inside <code>[]<\/code> to exclude a set or range. For example, the set <code>[^0-9]<\/code> matches any character except a digit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='alternation'>Alternation <a href=\"#alternation\" class=\"anchor\" id=\"alternation\" title=\"Anchor for Alternation\">#<\/a><\/h3>\n\n\n\n<p>Use <code>|<\/code> to represent alternation, which is like the OR operator. For example, the regular expression <code>a|b<\/code> matches a or b.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-regular-expression-operators'>PostgreSQL Regular Expression Operators <a href=\"#postgresql-regular-expression-operators\" class=\"anchor\" id=\"postgresql-regular-expression-operators\" title=\"Anchor for PostgreSQL Regular Expression Operators\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL allows you to use POSIX regular expressions with the following operators:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>~<\/code> matches a pattern.<\/li>\n\n\n\n<li><code>~*<\/code> case-insensitively matches a pattern.<\/li>\n\n\n\n<li><code>!~<\/code> does not match a pattern.<\/li>\n\n\n\n<li><code>!~*<\/code> case-insensitively does not match a pattern.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-regular-expression-examples'>PostgreSQL Regular Expression Examples <a href=\"#postgresql-regular-expression-examples\" class=\"anchor\" id=\"postgresql-regular-expression-examples\" title=\"Anchor for PostgreSQL Regular Expression Examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s explore some examples of using regular expressions with the <code>products<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"159\" height=\"254\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" alt=\"PostgreSQL Regular Expressions - Products Table\" class=\"wp-image-1051\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='matching-character-classes'>Matching Character Classes <a href=\"#matching-character-classes\" class=\"anchor\" id=\"matching-character-classes\" title=\"Anchor for Matching Character Classes\">#<\/a><\/h3>\n\n\n\n<p>The following query uses the match operator (<code>~<\/code>) to find products whose names contain three digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~ <span class=\"hljs-string\">'\\d{3}'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BICdcZHszfSc7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">      product_name\n<span class=\"hljs-comment\">-------------------------<\/span>\n Samsung QN900C Neo QLED\n Sony HT-A7000 Soundbar\n HP Spectre x360<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following query uses the match operator (<code>~<\/code>) to find products whose names contain the letter S followed by two digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~ <span class=\"hljs-string\">'S\\d{2}'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BICdTXGR7Mn0nOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">    product_name\n<span class=\"hljs-comment\">--------------------<\/span>\n Samsung Galaxy S24<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='matching-positions'>Matching Positions <a href=\"#matching-positions\" class=\"anchor\" id=\"matching-positions\" title=\"Anchor for Matching Positions\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the match operator (<code>~<\/code>) to find products with names starting with the letter H:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~ <span class=\"hljs-string\">'^H'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BICdeSCc7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">  product_name\n<span class=\"hljs-comment\">-----------------<\/span>\n Huawei Mate <span class=\"hljs-number\">60<\/span>\n HP Spectre x360<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following query uses the match operator (<code>~<\/code>) to find products with names ending with two digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~ <span class=\"hljs-string\">'\\d{2}$'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BICdcZHsyfSQnOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">    product_name\n<span class=\"hljs-comment\">--------------------<\/span>\n Samsung Galaxy S24\n Apple iPhone <span class=\"hljs-number\">15<\/span>\n Huawei Mate <span class=\"hljs-number\">60<\/span>\n Xiaomi Mi <span class=\"hljs-number\">14<\/span>\n Dell XPS <span class=\"hljs-number\">15<\/span>\n HP Spectre x360\n Dell Inspiron <span class=\"hljs-number\">27<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='matching-iphone-or-galaxy'>Matching iPhone or Galaxy <a href=\"#matching-iphone-or-galaxy\" class=\"anchor\" id=\"matching-iphone-or-galaxy\" title=\"Anchor for Matching iPhone or Galaxy\">#<\/a><\/h3>\n\n\n\n<p>The following query uses the match operator (<code>~<\/code>) to find products with names that contain &#8220;iPhone&#8221; or &#8220;Galaxy&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~ <span class=\"hljs-string\">'iPhone|Galaxy'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BICdpUGhvbmV8R2FsYXh5Jzs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       product_name\n<span class=\"hljs-comment\">---------------------------<\/span>\n Samsung Galaxy S24\n Apple iPhone <span class=\"hljs-number\">15<\/span>\n Samsung Galaxy Z Fold <span class=\"hljs-number\">5<\/span>\n Apple iPhone <span class=\"hljs-number\">15<\/span> Pro Max\n Samsung Galaxy Tab S9\n Samsung Galaxy Buds Pro <span class=\"hljs-number\">2<\/span>\n Samsung Galaxy Watch <span class=\"hljs-number\">6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To match &#8220;iPhone&#8221; and &#8220;Galaxy&#8221; case-insensitively, you can use the <code>~*<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name ~* <span class=\"hljs-string\">'iphone|galaxy'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSB%2BKiAnaXBob25lfGdhbGF4eSc7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       product_name\n<span class=\"hljs-comment\">---------------------------<\/span>\n Samsung Galaxy S24\n Apple iPhone <span class=\"hljs-number\">15<\/span>\n Samsung Galaxy Z Fold <span class=\"hljs-number\">5<\/span>\n Apple iPhone <span class=\"hljs-number\">15<\/span> Pro Max\n Samsung Galaxy Tab S9\n Samsung Galaxy Buds Pro <span class=\"hljs-number\">2<\/span>\n Samsung Galaxy Watch <span class=\"hljs-number\">6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='finding-product-names-that-do-not-match-a-pattern'>Finding Product Names that Do Not Match a Pattern <a href=\"#finding-product-names-that-do-not-match-a-pattern\" class=\"anchor\" id=\"finding-product-names-that-do-not-match-a-pattern\" title=\"Anchor for Finding Product Names that Do Not Match a Pattern\">#<\/a><\/h3>\n\n\n\n<p>The following query uses the operator (<code>!~<\/code>) to find products with names that do not contain digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name !~ <span class=\"hljs-string\">'\\d+'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSBGUk9NIHByb2R1Y3RzIFdIRVJFIHByb2R1Y3RfbmFtZSAhfiAnXGQrJzs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">    product_name\n<span class=\"hljs-comment\">--------------------<\/span>\n Bose SoundLink Max<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PostgreSQL supports POSIX regular expressions. <\/li>\n\n\n\n<li>Use a regular expression operator to match a string against a pattern.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=regex\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1219\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Regular Expressions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1219\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Regular Expressions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PostgreSQL regular expressions to perform flexible text searches.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":75,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1219","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Regular Expressions<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL regular expressions to perform flexible text searches.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Regular Expressions\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL regular expressions to perform flexible text searches.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-03T07:28:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" \/>\n\t<meta property=\"og:image:width\" content=\"159\" \/>\n\t<meta property=\"og:image:height\" content=\"254\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/\",\"name\":\"PostgreSQL Regular Expressions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"datePublished\":\"2024-12-23T10:47:53+00:00\",\"dateModified\":\"2025-01-03T07:28:18+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL regular expressions to perform flexible text searches.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"width\":159,\"height\":254,\"caption\":\"PostgreSQL Expression Index\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-regular-expressions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Regular Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL Regular Expressions","description":"In this tutorial, you'll learn how to use the PostgreSQL regular expressions to perform flexible text searches.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Regular Expressions","og_description":"In this tutorial, you'll learn how to use the PostgreSQL regular expressions to perform flexible text searches.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-03T07:28:18+00:00","og_image":[{"width":159,"height":254,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/","name":"PostgreSQL Regular Expressions","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","datePublished":"2024-12-23T10:47:53+00:00","dateModified":"2025-01-03T07:28:18+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL regular expressions to perform flexible text searches.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","width":159,"height":254,"caption":"PostgreSQL Expression Index"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Regular Expressions"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1219"}],"version-history":[{"count":4,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1219\/revisions"}],"predecessor-version":[{"id":1401,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1219\/revisions\/1401"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}