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 8419053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:30:12+00:00 2026-06-10T02:30:12+00:00

I use Java 7. I would like to extract the lang and the country

  • 0

I use Java 7.

I would like to extract the lang and the country from a String which represent a bundle file name or properties file name. File name doesn’t contains the extension.

For example

  • bundle –> Empty string or null
  • bundle_en –> en
  • bundle_en_US –> en_US
  • complicated_bundle_name_en_US –> en_US

I tried this but it doesn’t give me the expected result.

private static void testPattern(String bundleName) {
        final Pattern pattern = Pattern.compile(".+(_[a-z]{2,3}(_[A-Z]{2,3}){0,1}){0,1}");
        final Matcher matcher = pattern.matcher(bundleName);
        if (matcher.matches()) {
            for (int i = 0; i < matcher.groupCount(); ++i) {
                System.out.println("Group " + i  + " = " + matcher.group(i));
            }
        } else {
            System.out.println("Nothing");
        }
    }

For “bundle_en_US” its show:

Group 0 = bundle_fr_US
Group 1 = null

Can you help me to correct my regex or may be this regex already exist in the java core ?

Thanks.

  • 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-10T02:30:14+00:00Added an answer on June 10, 2026 at 2:30 am

    Problem is imo that .+ in the beginning already matches the whole sequence. Use reluctant quantifier instead:

    .+?(_[a-z]{2,3}(_[A-Z]{2,3}){0,1}){0,1}
    

    Edit: The finest solution I would consider this:

    .+?(?:_([a-z]{2,3})(?:_([A-Z]{2,3}))?)?
    

    Using non capturing groups this solely captures language and country code and throws out the “_”. Additionally you should change your for condition to i <= matcher.groupCount() otherwise you miss the last group. Using the last version and the altered for condition you get:

    Input: bundle
    Output:

    Group 0 = bundle
    Group 1 = null
    Group 2 = null
    

    Input: bundle_en
    Output:

    Group 0 = bundle_en
    Group 1 = en
    Group 2 = null
    

    Input: bundle_en_US
    Output:

    Group 0 = bundle_en_US
    Group 1 = en
    Group 2 = US
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use PKCS7 encryption to encode a value together with Java
There's a Java library that I would like to use in my project. My
I am using the java MongoDB driver for gridfs and would like to use
I have downloaded Eclipse Java EE version (3.5) and I would like to use
I have an array of Integers in Java, I would like use only a
How can I get output from Java anonymous classes? In .Net I would use
I would like to use java xml signing api for xades-t signature type. I
My XML looks like: <key>A</key> How do I extract the string A from the
Say I have a Java String which has xml data like so: String content
I would like to use java to get the source of a website (secure)

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.