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 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

Trying to use Java's regexp I want to match /app, /app/.* , but not
I'm trying to use Java regexps to match a pattern that spans multiple lines.
I'm trying to get a regex that will match: somefile_1.txt somefile_2.txt somefile_{anything}.txt but not
I am trying to use the following code, which I have not been able
I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
I'm trying to match SHA1's in generic text with a regular expression. Ideally I
I'd trying to style my ComboBoxes to match the rest of the UI but
I'm trying to come up with a Java regex that will match a filename
I am trying to match elements that have no other children elements, but also
Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey

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.