Results 1 to 10 of 10
  1. #1
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Regular Expression that displays locale information

    I have 2 questions based on Regular expressions, and I'm stuck through the second.

    2) Write a regular expression that displays only the following Locale information from all the available Locales. The information shows the displayname, a three-letter abbreviation for the locale's country and a three-letter abbreviation for the locale's language. Your program should verify your regular expression by printing out the information below.

    English (Malta),MLT,eng
    French (Belgium),BEL,fra
    French (Canada),CAN,fra
    French (Switzerland),CHE,fra
    English (Philippines),PHL,eng
    English (Ireland),IRL,eng
    French (Luxembourg),LUX,fra
    English (India),IND,eng
    English (Australia),AUS,eng
    French (France),FRA,fra
    English (Canada),CAN,eng
    English (Singapore),SGP,eng
    I'm confused as to where to even start.

    Question 1 required me to write a regular expression matching all the variations of my name, and asking the user to enter a string and testing if it matches; which I managed to complete.

  2. #2
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    EDIT: After a long search I'm guessing I need something along the lines of:

    Java Code:
    Locale[] locales = Calendar.getAvailableLocales();
            for(Locale l: locales)
            {
             if (l.matches("English|French.*)
                System.out.println(l);
            }

  3. #3
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    Any help please? It would be really appreciated :/

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Regular Expression that displays locale information

    The list of locales you displayed, is that the input or the desired output?

  5. #5
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    Quote Originally Posted by quad64bit View Post
    The list of locales you displayed, is that the input or the desired output?
    It's the desired output :)

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Regular Expression that displays locale information

    So, what does the input look like?

  7. #7
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    Quote Originally Posted by quad64bit View Post
    So, what does the input look like?
    If I knew that there wouldn't be a question :P - That's what I need to write :)

    The input would be all the available locales --> using a regular expression --> go through the list of locales and display only the locales as the output above.

    :/

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Regular Expression that displays locale information

    Have you gone through the API for the Locale class? Which methods do you see that would be useful in obtaining the inputs for your regex?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    The input for all the available locales is:

    Java Code:
    Locale locs[] = Locale.getAvailableLocales();
            
            for (Locale loc: locs)
            {
                System.out.print(loc.getDisplayName() + ",");
                System.out.print(loc.getISO3Country() + ",");
                System.out.println(loc.getISO3Language());
            }
    so I need to use a regular expression to sort through those lists and only display the locales that match these:

    English (Malta),MLT,eng
    French (Belgium),BEL,fra
    French (Canada),CAN,fra
    French (Switzerland),CHE,fra
    English (Philippines),PHL,eng
    English (Ireland),IRL,eng
    French (Luxembourg),LUX,fra
    English (India),IND,eng
    English (Australia),AUS,eng
    French (France),FRA,fra
    English (Canada),CAN,eng
    English (Singapore),SGP,eng
    Currently displays:

    Malay (Malaysia),MYS,msa
    Arabic (Qatar),QAT,ara
    Icelandic (Iceland),ISL,isl
    Finnish (Finland),FIN,fin
    Polish,,pol
    English (Malta),MLT,eng
    Italian (Switzerland),CHE,ita
    Dutch (Belgium),BEL,nld
    Arabic (Saudi Arabia),SAU,ara
    Arabic (Iraq),IRQ,ara
    Spanish (Puerto Rico),PRI,spa
    Spanish (Chile),CHL,spa
    Finnish,,fin
    German (Austria),AUT,deu
    Danish,,dan
    English (United Kingdom),GBR,eng
    Spanish (Panama),PAN,spa
    Serbian,,srp
    Arabic (Yemen),YEM,ara
    Macedonian (Macedonia),MKD,mkd

    ......

  10. #10
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Regular Expression that displays locale information

    In the very rare case that someone actually wondered what the answer was, I got there in the end:

    Java Code:
    public static void matchlocales()
        {
            Locale locs[] = Locale.getAvailableLocales();
            
            for (Locale loc: locs)
            {
                if ((loc.getDisplayName().matches("English.[^UN]*|French.[^,]*"))
                        && (loc.getISO3Country().matches("[^Z].*")))
                {
                    System.out.print(loc.getDisplayName() + ",");
                    System.out.print(loc.getISO3Country() + ",");
                    System.out.println(loc.getISO3Language());
                }
            }
        }

Similar Threads

  1. Regular expression
    By Krik in forum New To Java
    Replies: 3
    Last Post: 10-26-2012, 05:06 PM
  2. Regular expression
    By garnaout in forum New To Java
    Replies: 4
    Last Post: 05-15-2012, 06:22 PM
  3. Replies: 2
    Last Post: 07-26-2011, 07:48 AM
  4. Regular Expression Help
    By niketanand in forum Advanced Java
    Replies: 1
    Last Post: 06-24-2011, 04:56 PM
  5. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •