I need to get a substring from a path directory made of 2 numbers preceeded and followed by a '_'. The string is like:
'P:\pgdfecol\71698384737978\INFENTECONTROL\2011_9_43\2011_9_46_43_29_10.ZIP'
and in this case I want to get the 43 following the 46.
The path is stored following the next rule, after the last backslash:
'\TablesPK_twoCharactersClassification_twoCharactersDocumentType_anything.ZIP'
I want to get the classification. The catch is that the table's primary key could be more than one field, although I know in each case how many fields are in the pk.
I got with something like this:
select substring(substring(substring('P:\pgdfecol\71698384737978\INFENTECONTROL\2011_9_43\2011_9_46_43_29_10.ZIP' from '([^\\]*(\.ZIP|zip))') from '([^_]*_){4}') from '[0-9]{2}')
But I would like something simpler.
Other cases:
'P:\pgdfecol\71698384737978\INFENTECONTROL\2011_03_46\2011_03_46_46_48_.ZIP'
(need the second 46)
'P:\pgdfecol\71698384737978\INFCONTABLE\2009_05_INBP\2009_05_INBP_22_28_.ZIP'
(need the 22 after INBP)
'P:\pgdfecol\71698384737978\INFOFICIAL\2007_06_MB\2007_06_MB_29_28_.ZIP'
(need the 29)
'P:\pgdfecol\71698384737978\ASOCIADOS\8010625\8010625_02_04_20110111.ZIP'
(02 after 8010625)
In the last case, the pk is only one field, so I've changed the sentence as:
select substring(substring(substring('P:\pgdfecol\71698384737978\ASOCIADOS\8010625\8010625_02_04_20110111.ZIP' from '([^\\]*(\.ZIP|zip))') from '([^_]*_){2}') from '[0-9]{2}')
For one Pk, I need the second set of ([^_]*_)
, for three the fourth, and so on..
select substring(substring(substring('P:\pgdfecol\71698384737978\ACTASCOMITE\ACRE123\ACRE123_17_11_.ZIP' from '([^\\]*(\.ZIP|zip))') from '([^_]*_){2}') from '[0-9]{2}')
(I get 17)
I'm using postgres 9.0.
SHOW standard_conforming_strings;
, please. – Erwin Brandstetter Oct 26 '12 at 4:14_
before.ZIP
in8010625_02_04_20110111.ZIP
? – Erwin Brandstetter Oct 26 '12 at 4:47_
and digits between the two digits and.zip
? I think I got it all in my solution now. – Erwin Brandstetter Oct 26 '12 at 4:54