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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:03:58+00:00 2026-06-01T09:03:58+00:00

There is something I don’t understand in Java’s regular expressions. I have the following

  • 0

There is something I don’t understand in Java’s regular expressions. I have the following string (and I need the “to Date”):

From Date :01/11/2011 To Date :30/11/2011;;;;;;;;;;;;;

I think that the following regular expression (in Perl) would have matched.

to\\s+date\\s*?:\\s*?([0-9]{2}[\\./][0-9]{2}[\\./][0-9]{2,4})

In Java, this pattern doesn’t match. But it does if I add in front and at the end a .+
So this pattern works in Java:

Pattern p = Pattern.compile(".+to\\s+date\\s*?:\\s*?([0-9]{2}[\\./][0-9]{2}[\\./][0-9]{2,4}).+", Pattern.CASE_INSENSITIVE);

What I don’t understand: It would be clear to me that the first pattern would not match in Java if I add a ^ (beginning of the line) and a $ at the end of the line. That would mean, that the pattern has to match the whole line. But without that, the first pattern should actually match, because why does the pattern care about string data which is out of scope of this pattern, if I don’t set delimiters in front and at the end? This is not logical to me. In my opinion the first pattern should behave similar to the “contains” method of String class. And I think it is so in Perl.

  • 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-06-01T09:03:59+00:00Added an answer on June 1, 2026 at 9:03 am

    In Java, matches() validates the entire string. Your input probably has line breaks in them (which don’t get matched by .+).

    Try this instead:

    Pattern p = Pattern.compile(".+to\\s+date\\s*?:\\s*?([0-9]{2}[\\./][0-9]{2}[\\./][0-9]{2,4}).+", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher("... \n From Date :01/11/2011 To Date :30/11/2011;;;;;;;;;;;;; \n ...");
    
    System.out.println(m.matches()); // prints false
    
    if(m.find()) {
      System.out.println(m.group(1)); // prints 30/11/2011
    }
    

    And when using find(), your can drop the .+‘s from the pattern:

    Pattern.compile("to\\s+date\\s*?:\\s*?([0-9]{2}[./][0-9]{2}[./][0-9]{2,4})", Pattern.CASE_INSENSITIVE);
    

    (no need to escape the . inside a character class, btw)

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

Sidebar

Related Questions

I'm beginning and there is something that I don't understand with pointers. I have
There's something I don't understand: I computes the spectral density of a signal (by
There's something I don't quite understand with how references are handled in C++: B
I think there is something I don't understand about same origin limitation for XMLHttpRequest
I'm trying to learn MVVM, but there is something I don't understand yet. Currently,
There is something that I really don't understand with the HttpListener. The code below
There is something I don't understand, When I don't put certificate at all, the
Looking at the IDisposable pattern + Finalizer pattern, there is something I don't understand:
I'm following Scott Guthries MVC tutorial ( http://nerddinnerbook.s3.amazonaws.com/Part6.htm ) and there's something I don't
There's something I don't understand about neural networks. I've tried to use them with

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.