0

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

1
  • You're actually right. I'll change it to better comply with the rules Commented May 31, 2015 at 14:01

1 Answer 1

3

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

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

 regexp_replace 
----------------
 8552525
1
  • I like the posix variant of the same thing: regexp_replace('dsa8a552a5a2a5?', '[^[:digit:]]','','g') Commented Jun 8, 2015 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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