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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:34:12+00:00 2026-06-14T04:34:12+00:00

I am quite bad at Java Regular expression so I hope you guys will

  • 0

I am quite bad at Java Regular expression so I hope you guys will help me.

String variable = "My life is better ";
String variable2 = "My life01 is better";

Now I have to write a code which would return true if the string has only “life”

So I should get TRUE only for variable not for variable2 because it has life but “01” too.

~thanks.

I have tried

if (variable.contains("life")){
System.out.println("TRUE");}

It return TRUE for both.

See solution :

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Pattern p = Pattern.compile("\\blife\\b");
        Matcher m = p.matcher("life0 is better");
        boolean b = m.find();
        System.out.println(b);

    }


}
  • 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-14T04:34:14+00:00Added an answer on June 14, 2026 at 4:34 am

    Use the following regex: –

    "\blife\b"
    

    with Pattern and Matcher class. This will match for complete word. (\b denote word boundary)

    You would have to use Matcher#find method, to check whether a string contains this pattern.


    Note: – If you want to use String.matches, which would be appropriate here, than going with Pattern and Matcher, you would have to add .* in the front and the end. Because, String.matches matches the whole string.

    For e.g: –

    String str = "asdf life asdf";
    System.out.println(str.matches("\\blife\\b"));       // Prints false
    
    System.out.println(str.matches(".*\\blife\\b.*"));  // Prints true
    

    In the second Regex, .* matches the string before and after life.

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

Sidebar

Related Questions

After playing quite a bit of Bad Company 2 over the last month, I'm
Sorry, I'm quite new to Java. I've stumbled across HttpGet and HttpPost which seem
I'm not quite understanding why there are finalizers in languages such as java and
I am making a program that will have the ability to run the java
I have been reading about streams in Java the past days. After reading quite
I'm currently developing a server (using Java) that has to send quite often (every
I understand the basic idea of java's String interning, but I'm trying to figure
I think the title of the question is quite bad explained so I'll try
I'm French so my English is quite bad but I have a real problem
I'm quite bad with regex, and I'm looking to match a criteria. This is

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.