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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:56:33+00:00 2026-05-30T12:56:33+00:00

Consider the following code snippet: String input = Print this; System.out.println(input.matches(\\bthis\\b)); Output false What

  • 0

Consider the following code snippet:

String input = "Print this";
System.out.println(input.matches("\\bthis\\b"));

Output

false

What could be possibly wrong with this approach? If it is wrong, then what is the right solution to find the exact word match?

PS: I have found a variety of similar questions here but none of them provide the solution I am looking for.
Thanks in advance.

  • 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-05-30T12:56:35+00:00Added an answer on May 30, 2026 at 12:56 pm

    When you use the matches() method, it is trying to match the entire input. In your example, the input “Print this” doesn’t match the pattern because the word “Print” isn’t matched.

    So you need to add something to the regex to match the initial part of the string, e.g.

    .*\\bthis\\b
    

    And if you want to allow extra text at the end of the line too:

    .*\\bthis\\b.*
    

    Alternatively, use a Matcher object and use Matcher.find() to find matches within the input string:

        Pattern p = Pattern.compile("\\bthis\\b");
        Matcher m = p.matcher("Print this");
        m.find();
        System.out.println(m.group());
    

    Output:

    this
    

    If you want to find multiple matches in a line, you can call find() and group() repeatedly to extract them all.

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

Sidebar

Related Questions

Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code snippet below. class X { public String toString() { return
Consider the following code snippet namespace ConsoleApplication1 { public delegate TResult Function<in T, out
Consider the following code snippet that takes user input (a date) and format it
Consider the following code snippet: #!/bin/bash #This program tests the if statement in bash.
Scenario Consider the following code snippet. string s = S; string s1 = S;
Consider the following Code snippet, Assembly asm = Assembly.LoadFile(DLL_Path); Type t = asm.GetType(DLL_NameSpace.MyClass, false,
Consider the following code snippet: bool SomeObject::equal(const SomeObject& rhs) const { if (this ==
Consider the following code snippet: $beat = date('B'); // 1 beat = 86.4 seconds,
Consider the following snippet of code: // get number of sheep in DataTable by

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.