How to use backlinks beyond 9 in a PostgreSQL regex?

The following regular expression simply extracts a piece of matching text using callback numbers:

SELECT regexp_replace('ABCDEFGHIJ','(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)','\2');
B

But how to draw a conclusion beyond the ninth matched substring?

The following actions will not work (returns the first match + 0), and use 10 or $ {10}:

SELECT regexp_replace('ABCDEFGHIJ','(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)','\10')
A0

Note. This example was simplified for clarity and, of course, did not necessarily go beyond the backlink.

+4
source share
3 answers

As I said in my comment and in @pswg's answer, according to the documentation , this is not possible.

RegEx, . , . , , regexp_matches().

SELECT matches[10]
FROM regexp_matches('ABCDEFGHIJ', '(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)') AS matches;

 matches 
---------
 J
(1 row)
+2

, 9:

\mnn ( m - , nn - , mnn , ), mnn 'th

, , regexp_replace:

replacement \n, n 1 9, , , n 'th, , ​​& hellip;

+1

I would prefer the approach mentioned by @Sam in general , but created a nice , clean sql wrapper function > 9 backlink support here: postgres-utils regexp_matches(...) regexp_replace2(...)

0
source

All Articles