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

  • You're actually right. I'll change it to better comply with the rules – Fabrizio Mazzoni May 31 '15 at 14:01

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

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

  • I like the posix variant of the same thing: regexp_replace('dsa8a552a5a2a5?', '[^[:digit:]]','','g') – Kirk Roybal Jun 8 '15 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.