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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:12+00:00 2026-05-26T11:41:12+00:00

I understand Java regular expressions can be accessed from the String ‘s matches convenience

  • 0

I understand Java regular expressions can be accessed from the String‘s matches convenience method, or going the long route and making a Pattern, etc. So, the following code should really print 2 “Yes!” lines to the output. It prints “Yes!” line and “no” line. What am I missing?

import java.util.regex.*;

public class TestRegex {
public static void main(String[] args) {
    String pattern = "html";
    String input   = "somehtml.txt";

    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(input);

    if(m.find()) {
        System.out.println("Yes!");
    }
    else {
        System.out.println("no");
    }

    if(input.matches(pattern)) {
        System.out.println("Yes!");
    }
    else {
        System.out.println("no");
    }

}

}

Output:

Yes!
no

Java version 1.6 on Win7 64-bit.

C:\Users\Michael Smith>java -version
java version “1.6.0_24”
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

  • 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-26T11:41:13+00:00Added an answer on May 26, 2026 at 11:41 am

    matches() checks for the whole string matching the regular expression. find() only looks for a match somewhere in the string.

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

Sidebar

Related Questions

Please understand firstly that I fully understand that Java will return a String when
In Java, flush() method is used in streams. But I don't understand what are
I'm new to regular expressions in Java (or any language, for that matter) and
I understand the following Java outputs. public class EchoTestDrive { public static void main(String[]
I understand that Java can load/execute DLL code, but I'm wondering if there are
I'm new to regular expressions and java so please bear with my newbish question.
Can any one please describe this sort of code to understand Java closure. public
I recently realized that I don't fully understand Java's string encoding process. Consider the
I read many articles to understand Java servlet but I did not succeed. Can
From what I understand Java packages often use a company's website as a package

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.