Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dobut in regex code

 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one let me know that why this is giving false ouptut.

____________________________________________________________________
Pattern pattern = Pattern.compile("DD-mm-YYYY-HH:MM:SS");
Matcher matcher = pattern.matcher("12-11-2008-11:11:11");
System.out.println(matcher.find());
_____________________________________________________________________

output - false

I could not understand..although i have checked mm/MM

_____________________
Mintoo
________________
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mintoo,

The pattern you're passing to compile is a regular expression; it's a string matching tool. Pattern doesn't look at is as a Date format but as a regular expression

That's why it's not matching.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're confusing regular expression patterns with the patterns used by the DateFormat classes. The regexp pattern would be something like "[0-3]\\d-[01]\\d-[0-2]\\d{3}-[0-2]\\d:[0-5]\\d:[0-5]\\d". Have a look at the javadocs of the java.util.regex.Pattern class for details.

If you want to check that a string is a valid date/time pattern, have a look at the java.text.SimpleDateFormat.parse method.
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though i have changed it ,like
Pattern pattern = Pattern.compile("DD-mm-YYYY-HH:MM:SS");
but still not able to get the anwser.

____________
Mintoo
SCJP 1.4
_____________
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf , you are correct.I know that.
But i wonder why this pattern is not working .this is also java standard pattern and best suited for the dateformat.

This is basic pattern for Dateformat.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mintoo kumar:
This is basic pattern for Dateformat.


Yup, but interpreted as a regex, which is what Pattern.compile expects as a parameter, it's not what you want.

Ulf has already given you a candidate regex for your need, why don't you test it, and fine tune if needed.

For more information on regular-expression constructs, see the API for java.util.regex.Pattern.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is also java standard pattern

There is no such thing as a "Java standard pattern". Regular expressions use one kind of pattern, date formats use a different one, PrintStream.printf uses yet another format, etc. None of these formats have anything to do with the other ones, and can't be used interchangeably with any of them.
[ May 06, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic