Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8748663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:33:54+00:00 2026-06-13T12:33:54+00:00

I need to get a substring from a path directory made of 2 numbers

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:33:55+00:00Added an answer on June 13, 2026 at 12:33 pm

    I am beginning to understand. Consider this test case:

    WITH x(txt) AS ( VALUES
         ('P:\pgdfecol\71698384737978\INFENTECONTROL\2011_9_43\2011_9_46_43_29_10.ZIP')  -- 43
        ,('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
        )
    SELECT txt, substring(txt, '\\(?:[^_\\]+_){3}(\d\d)_[^\\]*\.(?:ZIP|zip)$')
    FROM   x
    

    (?:) .. non-capturing parenthesis
    [^_\\].. character class with any character except \ and _
    \d .. a digit, same as [0-9] effectively
    + .. 1 or more matches (greedy)
    $ .. end of string
    [_\d] .. character class with digits and _

    The case with only one pk needs a different pattern. Use {1} instead of {3}.

    Not sure why you escape the backslashes. In modern versions of PostgreSQL standard_conforming_strings is on by default so you don’t need to escape backslashes in strings – but still in regular expression of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get substring from a string where it will give output of
I need to get all substrings from string. For ex: StringParser.GetSubstrings([start]aaaaaa[end] wwwww [start]cccccc[end], [start],
I need to pull a specific substring from a string of the form: foo=abc;bar=def;baz=ghi
I need to get the proper value of hours from a time string. Here
I am trying to get the enclosing directory from a path: DsVersions.ASSEMBLY2Row row =
I need to get a value from a file path. assume my path looks
I'm looking to get a Substring from a String: The best way to explain
I am writing a macro in Excel where I need to get a substring
i've got stuck with substring. On input i've got a string that looks like
I have a NSMutableAttributedString, I need to take substring from it.I use the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.