I need to build a query in PostgreSQL and am required to find all text entries that contain a 6 digit number (e.g. 000999, 019290, 998981, 234567, etc). The problem is that the number is not necessary at the begining of the string or at its end.
I tried and didn’t work:
[0-9]{6}– returns part of a number with more than 6 digits(?:(?<!\d)\d{6}(?!\d))– postgresql does not know about lookbehind[^0-9][0-9]{6}[^0-9]and variations on it, but to no avail.
Building my own Perl/C function is not really an option as I do not have the skills required. Any idea what regexp could be used or other tricks that elude me at the moment?
EDIT
Input samples:
aa 0011527 /CASA-> should return NOTHINGaa 001152/CASA-> should return001152aa001152/CASA-> should return001152aa0011527/CASA-> should return NOTHINGaa001152 /CASA-> should return001152
If PostgreSQL supports word boundaries, use
\b:Edit:
\bin PostgreSQL meansbackspace, so it’s not a word boundary.http://www.postgresql.org/docs/8.3/interactive/functions-matching.html#FUNCTIONS-POSIX-REGEXP however, will explain you that you can use
\yas a word boundary, as it meansmatches only at the beginning or end of a word, soshould work.
should also work.
Full list of word matches in PostgreSQL regex:
New edit:
Based on your edit, you should be able to do this: