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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:10:35+00:00 2026-06-09T19:10:35+00:00

I have a line from which multiple keywords are to be matched. The whole

  • 0

I have a line from which multiple keywords are to be matched. The whole keywords should be matched.

Example,

 String str = "This is an example text for matching countries like Australia India England";

 if(str.contains("Australia") ||
    str.contains("India") ||
    str.contains("England")){
    System.out.println("Matches");
 }else{
    System.out.println("Does not match");
 }

This code works fine. But if there are too many keywords to be matched, the line grows. Is there any elegant way of writing the same code?
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-09T19:10:37+00:00Added an answer on June 9, 2026 at 7:10 pm

    Your can write a regular expression like this:

    Country0|Country1|Country2
    

    Use it like this:

    String str = "This is an example text like Australia India England";
    
    if (Pattern.compile("Australia|India|England").matcher(str).find())     
        System.out.println("Matches");
    

    If you would like to know which countries has matched:

    public static void main(String[] args) {
    
        String str = "This is an example text like Australia India England";
    
        Matcher m = Pattern.compile("Australia|India|England").matcher(str);
        while (m.find())        
            System.out.println("Matches: " + m.group());
    }
    

    Outputs:

    Matches: Australia
    Matches: India
    Matches: England
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text view with multiple lines of text e.g. This is line
I have a file from which i read line by line and i have
I have an application which is built from command line (ANT) using J2ME Polish.
I have a project which builds correctly from the command line. On one of
I have an old, third party, command line, proprietary program which I'm calling from
I have an app, that uses backup feature. This is line from my manifest:
I'm trying to format a table from XML. Lets say I have this line
I am having a form in which I have multiple line textbox and status
I have a string (with multiple lines) which contains the following: Name=My name Address=......
I have a file which contains multiple lines of this format: Item A 62.7

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.