Home Explore Blog CI



postgresql

76th chunk of `doc/src/sgml/func.sgml`
9066e2ec76d2ee63409b53ffa42e301ce89c9c13aa8fb7420000000100000fab
 <literal>g</literal> flag which commands it to return all matches, not
     just the first one.
    </para>

   <para>
    Some examples:
<programlisting>
SELECT regexp_matches('foo', 'not there');
 regexp_matches
----------------
(0 rows)

SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g');
 regexp_matches
----------------
 {bar,beque}
 {bazil,barf}
(2 rows)
</programlisting>
   </para>

   <tip>
    <para>
     In most cases <function>regexp_matches()</function> should be used with
     the <literal>g</literal> flag, since if you only want the first match, it's
     easier and more efficient to use <function>regexp_match()</function>.
     However, <function>regexp_match()</function> only exists
     in <productname>PostgreSQL</productname> version 10 and up.  When working in older
     versions, a common trick is to place a <function>regexp_matches()</function>
     call in a sub-select, for example:
<programlisting>
SELECT col1, (SELECT regexp_matches(col2, '(bar)(beque)')) FROM tab;
</programlisting>
     This produces a text array if there's a match, or <literal>NULL</literal> if
     not, the same as <function>regexp_match()</function> would do.  Without the
     sub-select, this query would produce no output at all for table rows
     without a match, which is typically not the desired behavior.
    </para>
   </tip>

    <para>
     The <function>regexp_replace</function> function provides substitution of
     new text for substrings that match POSIX regular expression patterns.
     It has the syntax
     <function>regexp_replace</function>(<replaceable>string</replaceable>,
     <replaceable>pattern</replaceable>, <replaceable>replacement</replaceable>
     <optional>, <replaceable>flags</replaceable> </optional>)
     or
     <function>regexp_replace</function>(<replaceable>string</replaceable>,
     <replaceable>pattern</replaceable>, <replaceable>replacement</replaceable>,
     <replaceable>start</replaceable>
     <optional>, <replaceable>N</replaceable>
     <optional>, <replaceable>flags</replaceable> </optional></optional>).
     The source <replaceable>string</replaceable> is returned unchanged if
     there is no match to the <replaceable>pattern</replaceable>.  If there is a
     match, the <replaceable>string</replaceable> is returned with the
     <replaceable>replacement</replaceable> string substituted for the matching
     substring.  The <replaceable>replacement</replaceable> string can contain
     <literal>\</literal><replaceable>n</replaceable>, where <replaceable>n</replaceable> is 1
     through 9, to indicate that the source substring matching the
     <replaceable>n</replaceable>'th parenthesized subexpression of the pattern should be
     inserted, and it can contain <literal>\&amp;</literal> to indicate that the
     substring matching the entire pattern should be inserted.  Write
     <literal>\\</literal> if you need to put a literal backslash in the replacement
     text.
     <replaceable>pattern</replaceable> is searched for
     in <replaceable>string</replaceable>, normally from the beginning of
     the string, but if the <replaceable>start</replaceable> parameter is
     provided then beginning from that character index.
     By default, only the first match of the pattern is replaced.
     If <replaceable>N</replaceable> is specified and is greater than zero,
     then the <replaceable>N</replaceable>'th match of the pattern
     is replaced.
     If the <literal>g</literal> flag is given, or
     if <replaceable>N</replaceable> is specified and is zero, then all
     matches at or after the <replaceable>start</replaceable> position are
     replaced.  (The <literal>g</literal> flag is ignored
     when <replaceable>N</replaceable> is specified.)
     The <replaceable>flags</replaceable> parameter is an optional text
     string containing zero or more single-letter flags that change the
     function's behavior.  Supported flags (though
     not <literal>g</literal>)

Title: regexp_matches Usage and regexp_replace Function
Summary
This section continues discussing `regexp_matches` and offers a tip for using it in older PostgreSQL versions without `regexp_match` by using a sub-select. Then, it introduces the `regexp_replace` function for substituting text matching POSIX regular expression patterns. It details the syntax, including how to use backreferences to insert captured groups or the entire matched substring in the replacement text. It explains the function parameters like `string`, `pattern`, `replacement`, `start`, `N` and `flags` including how to replace multiple matches using the `g` flag or specifying a value for `N`.