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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:02:58+00:00 2026-05-29T06:02:58+00:00

The following code matches the two expressions and prints success. import java.util.regex.Matcher; import java.util.regex.Pattern;

  • 0

The following code matches the two expressions and prints success.

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

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

However, I want white space to not matter, so the following should also print success.

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

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id:[0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

The Pattern.COMMENTS flag is supposed to permit white space, but it causes Failure to be printed. It even causes Failure to be printed if the strings are exactly equivalent including white space, like in the first example. For example,

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

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

Prints Failure.

Why is this happening and how do I make the Pattern ignore white space?

  • 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-29T06:02:59+00:00Added an answer on May 29, 2026 at 6:02 am

    There is a misunderstanding on your side. Pattern.COMMENTS allow you to put additional whitespace into your regex, to improve the readability of the regex, but this whitespace will NOT be matched in the string.

    This does not allow whitespace in your string, that is then matched automatically, without being defined in the regex.

    Example

    With Pattern.COMMENTS you can put whitespace in your regex like this

    String regex = "\\{ user_id: [0-9]+ \\}";
    

    to improve readablitiy, but the it will not match the string

    String string = "{user_id : 0}";
    

    because you haven’t defined the whitespaces in the string, so if you want to use Pattern.COMMENTS then you need to treat whitespace you want to match specially, either you escape it

    String regex = "\\{ user_id\\ :\\  [0-9]+ \\}";
    

    or you use the whitespace class

    String regex = "\\{ user_id \\s:\\s [0-9]+ \\}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following code class One: scope = 'first_scope' class Two: scope
The following javascript code concatenates the matches found by a regular expression. The regular
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
The following code works: String str= test with foo hoo; Pattern pattern = Pattern.compile(foo);
I have the following code which tries to add a class of selected to
The following code number=1 if [[ $number =~ [0-9] ]] then echo matched fi
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666
The following code works great in IE, but not in FF or Safari. I

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.