PostgreSQL Regex – Match All Digits in Column

postgresqlpostgresql-9.4regex

How do I extract all digits from a string with regex in pgsql?

select regexp_matches('dsa8a552a5a2a5?', '\d+');

Will output only 8.

Need to get 8552525

Best Answer

Better use regexp_replace, globally replacing non-digits by an empty string:

select regexp_replace('dsa8a552a5a2a5?', '\D', '', 'g');

 regexp_replace 
----------------
 8552525