What you want does not have to do with REGEX since it is necessary to perform arithmetic operations.
Resolution
CREATE TABLE prefixos(
prefixo INTEGER
);
INSERT INTO prefixos (prefixo) VALUES
('429'),
('4299'),
('42999'),
('429998');
SELECT
P.prefixo,
min(P.prefixo - Q.pos)
FROM
prefixos P
LEFT JOIN (
SELECT prefixo,
substring('4299234180' from 0 for char_length(prefixo::varchar)+1)::integer as pos
FROM prefixos
) as Q ON Q.prefixo = P.prefixo
WHERE
(P.prefixo - Q.pos) > 0
GROUP BY
P.prefixo
ORDER BY
2
LIMIT 1
Explanation
To solve your problem I used the following logic:
- Capture the part of the number corresponding to the size of the prefix. Prefix
429
has len = 3, so I'll capture the first 3 characters of your number.
The part of does this is:
LEFT JOIN (
SELECT prefixo,
substring('4299234180' from 0 for char_length(prefixo::varchar)+1)::integer as pos
FROM prefixos
) as Q ON Q.prefixo = P.prefixo
- After having the corresponding number to the prefix, I perform an arithmetic operation to remove them equal to zero because it wants to say that a higher prefix should fit.
- Finally, I check as little variation as possible so that I have the prefix that fits best.
(P.prefixo - Q.pos) > 0
Note
- The base I used was PostgreSQL, you may need to adapt something, as not all DBMS have the same functions.