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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:03:41+00:00 2026-05-16T21:03:41+00:00

I am trying to match a multi line text using Java. When I use

  • 0

I am trying to match a multi line text using Java. When I use the Pattern class with the Pattern.MULTILINE modifier, I am able to match, but I am not able to do so with (?m).

The same pattern with (?m) and using String.matches does not seem to work. I am sure I am missing something, but no idea what.

This is what I tried:

String test = "User Comments: This is \t a\ta \n test \n\n message \n";
        
String pattern1 = "User Comments: (\\W)*(\\S)*";
Pattern p = Pattern.compile(pattern1, Pattern.MULTILINE);
System.out.println(p.matcher(test).find());  //true

String pattern2 = "(?m)User Comments: (\\W)*(\\S)*";
System.out.println(test.matches(pattern2));  //false - why?
  • 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-16T21:03:41+00:00Added an answer on May 16, 2026 at 9:03 pm

    First, you’re using the modifiers under an incorrect assumption.

    Pattern.MULTILINE or (?m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string).

    Pattern.DOTALL or (?s) tells Java to allow the dot to match newline characters, too.

    Second, in your case, the regex fails because you’re using the matches() method which expects the regex to match the entire string – which of course doesn’t work since there are some characters left after (\\W)*(\\S)* have matched.

    So if you’re simply looking for a string that starts with User Comments:, use the regex

    ^\s*User Comments:\s*(.*)
    

    with the Pattern.DOTALL option:

    Pattern regex = Pattern.compile("^\\s*User Comments:\\s+(.*)", Pattern.DOTALL);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        ResultString = regexMatcher.group(1);
    } 
    

    ResultString will then contain the text after User Comments:

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

Sidebar

Related Questions

I'm trying to match a string that looks something like this: <$Fexample text in
I'm trying to match out of this text: <a href=http://english317.ning.com/profiles/blogs/bad-business-writing-487>Continue</a> </div> <p class=small> Added
I am trying to match the beginning of strings in f#. Not sure if
I'm trying to implement a multi threaded, recursive file search logic in Visual C++.
I'm trying to match usernames from a file. It's kind of like this: username=asd123
I am trying to match a parent in the following HTML <tbody> <tr id=group-1
I'm trying to match parts of a larger block of content and want to
I am trying to develop a C++ application. Part of the Application is meant
I'm busy trying to create two regular expressions to filter the id from youtube

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.