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

  • SEARCH
  • Home
  • 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 7710299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:05:32+00:00 2026-06-01T01:05:32+00:00

File file = new File(file-type-string-i-want-2000-01-01-01-01-01.conf.gz); Matcher matcher = pattern.compile(\\-(.*)\\-\\d{4})).matcher(fileName); StringBuilder sb = new StringBuilder();

  • 0
File file = new File("file-type-string-i-want-2000-01-01-01-01-01.conf.gz");
            Matcher matcher = pattern.compile("\\-(.*)\\-\\d{4}")).matcher(fileName);
            StringBuilder sb = new StringBuilder();
            while (matcher.find()) {
                sb.append(matcher.group());
            }
            stringList = Arrays.asList(sb.toString().split("-"));
            if (stringList.size() >= 2) {
                nameFragment = stringList.get(stringList.size() - 2);
            }

Desired result is to extract

string-iwant 

from strings that look like this

file-type-string-iwant-2000-01-01-01-01-01.conf.gz 

Unfortunatly, the format for “string-iwant” is a non-fixed length of alpha-numeric characters that will include only ONE hyphen BUT never start with a hyphen. The date formatting is consistent, the year is always after the string, so my current approach is to match on the -year, but I’m having difficulty excluding the stuff at the beginning.

Thanks for any thoughts or ideas

Edit: updated strings

  • 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-01T01:05:34+00:00Added an answer on June 1, 2026 at 1:05 am

    Here’s the regex you need:

    \\-([^-]+\\-[^-]+)\\-\\d{4}\\-

    Basically it means:

    • - starts with minus
    • ([^-]+\\-[^-]+) contains 1 or more non-minus symbols, then a minus, then 1 or more non-minus symbols. This part is captured.
    • -\d{4} a minus sign and 4 digits

    However, that will only work if stuff-you-need has only one hyphen (or a constant amount of hyphens, which will need correction in regex). Otherwise, there is no way to know if given the string file-type-string-i-want the word type belongs to a sting you want or not.

    Added:

    In case the file-type always contains exactly one hyphen, you can capture the required part this way:

    [^-]+\\-[^-]+\\-(.*)\\-\\d{4}\\-

    Explanation:

    • [^-]+\-[^-]+\\- some amount of non-hyphen characters, then a hyphen, then more non-hyphens. This will skip the file-type string with the following hyphen.
    • \-\d{4}\- a hyphen, 4 digits followed by another hyphen
    • (.*) everything in between of previous 2 statements is captured as being the string you need to select
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to let the user type in the name of a new file,
When I try to do this ( real code ): fh = File.new(a file
In VS2008, I go to File/New/Project, select ASP.NET Web Application and create a new
There is a new file [project-name].otares created in Delphi XE2. The wiki says: File
Quite simply: File account = new File(./data/account); account.createNewFile(); Gives me: java.io.IOException: No such file
When creating a new file with vim, I would like to automatically add some
I added a new file F1 and made changes to another file F2 but
I just did File -> New Project last night on a new project. Ah,
When Eclipse creates a new file ( .c or .h file) in a C
I'm trying to output a xml file without xml head like I tried: Type

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.