I use the Postgres regexp_matches function to extract numbers.
The regular expression I use is
4([\\s\\-\\/\\.]*?0){3}([\\s\\-\\/\\.]*?[
The regexp_matches(string text, pattern text [, flags text]) function returns the captured values:
Return all captured substrings resulting from matching a POSIX regular expression against the string.
You may fix the expression using non-capturing groups:
SELECT unnest(regexp_matches('4-0001-1234 4.0001.12344 4-0-0-0-1-1234', '4(?:[\s/.-]*0){3}(?:[\s/.-]*[12])(?:[\s/.-]*\d){4}', 'g'));
See the online demo.
BTW, you do not need to escape -
when it is at the start/end of the bracket expression, and there is no need to escape neither /
nor .
there. I also suggest removing {1}
as a
= a{1}
in any regex supporting limiting quantifiers.