CYBERTEC PostgreSQL Logo

Using regular expressions and arrays in PostgreSQL

06.2025 / Category: / Tags: |

Regular expressions and PostgreSQL have been a great team for many many years. The same is true for PostgreSQL arrays, which have been around for a long time as well. However, what people rarely do is combine those two technologies into something more powerful that can be used for various purposes.

Using ANY and ALL

One of the most widely adopted ways of dealing with arrays is the idea of ANY and ALL, which has been supported by PostgreSQL since God knows when. Those two keywords allow us to figure out if a certain value and an array are a match.

Here are some examples:

The first statement simply generates an array of numbers, which can be used in my example to demonstrate how ANY and ALL work. The idea is simple: ANY will check if one of the values in the array matches - ALL will check if all of the values are a match. So far this is quite common.

However, what happens if we try to apply this concept to regular expressions?

PostgreSQL and regular expressions

Many readers might be surprised to learn that combining those two techniques is indeed possible and actually fairly straight forward. Note that in the example above ANY and ALL were essentially used in combination with the = operator. However, we can also apply the ~ operator, which is the PostgreSQL way of handling regular expressions:

What it essentially does is matching the regular expression on the right hand side of the operator with the string on the left. So far so good, but what happens if we expand on this a bit?

Here is what we can do:

What we have just created is an array of regular expressions that we can easily apply:

Voilà, we can see that all expressions have matched successfully. All we had to do was utilize ~ALL instead of =ALL to do the trick. While this might be obvious to some, it does not seem to be common knowledge out there.

Finally ...

If you want to learn more about pattern matching and fuzzy search, consider reading my blog post dealing with this topic and hopefully learn more about some of those techniques.

2 responses to “Using regular expressions and arrays in PostgreSQL”

  1. Too bad IS [NOT] DISTINCT FROM is not an operator

    ERROR: syntax error at or near "any"
    LINE 1: select 'a' is not distinct from any ('{a,b,c}'::text[]);

    • I have been wishing for that too. In particular, an operator could at least potentially use an index.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2025
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram