aspose file tools*
The moose likes Beginning Java and the fly likes Dobut in regex code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dobut in regex code" Watch "Dobut in regex code" New topic
Author

Dobut in regex code

Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
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

Joined: Mar 22, 2008
Posts: 265

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.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
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

Joined: Aug 21, 2007
Posts: 61
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

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dobut in regex code