aspose file tools*
The moose likes Beginning Java and the fly likes Date and time search using regular expression. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date and time search using regular expression." Watch "Date and time search using regular expression." New topic
Author

Date and time search using regular expression.

Ashwin Raghavan
Ranch Hand

Joined: Dec 26, 2012
Posts: 30
Hi..

i have some files which are created on daily basis. they have the following format. <Name1>_<Name2>_yyyymmdd_hh:mm:ss.<extension>.


Now i am trying to read these set of files from[ java. can some body tell me the regex i should be using to match the file names.


I tried using ^.*[0-9].*{8}_[0-1]{2}:?[0-9]{2}:?[0-9]:?\\.<extension>$


This is not working.

Let me kwow how to make this work.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

for starters, the {8} isn't valid where you have it.

What you wrote says

beginning of line (^) followed by
any number of characters (.*) followed by
a single digit 0-9 ([-9]) followed by
any number of characters (.*) repeated up to eight times ({8}) <-----

how can you repeat any number of characters up to eight times?

skipping a bit...

What is this supposed to match:

[0-1]{2}

you are saying "exactly two characters that are 0 or 1"...I'm not sure what you are trying to match there...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ashwin Raghavan
Ranch Hand

Joined: Dec 26, 2012
Posts: 30
All i want to do is search the directory for files which have the pattern "<Name1>_<Name2>_yyyymmdd_hh:mm:ss.<extension> "

Am sorry, There was typo in my above expression. It was .*[0-9]{8}.
Neverthless I tried with the below one. It did not work.
.*[0-9]{8}_?\\d{2}:?\\d{2}:?\\d{2}\\.<File Extension>$

I am using this in pattern.compile(<above String>). Let me know what i am doing wrong. and how to match the expression.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

ok...i think your basic regex is correct. I have a tool called "the regex coach" that lets one play with regexes and target strings.

this pattern:

.*[0-9]{8}_?\d{2}:?\d{2}:?\d{2}.<extension>

matches this target:

name1_name2_20121221_14:22:29.<extension>

I assume you have the double backslashes in your regex due to it being in a quoted string. I know that sometimes you have to do strange things and put in extra slashes (up to four), but I think that is when you need a literal backslash in your ultimate regex.

I would suggest this. Shorten your regex. Cut off everything after the first {8}. See if that matches correctly. You may get some 'extra' matches, but make sure it is matching correctly for what it is. IF it works, add the next atomic unit, like the "_?". See if that matches correctly. Then keep building up your regex a piece at a time until you find the piece that breaks it. Then focus on just that part.
Ashwin Raghavan
Ranch Hand

Joined: Dec 26, 2012
Posts: 30
Thanks Fred.

You are right. I used double backslashes in my regex due to it being in a quoted string.

I found a website to test the java regex expressions.

http://www.regexplanet.com/advanced/java/index.html

This is the final string i came up with

^.*[0-9]{8}_?\\d{2}:?\\d{2}:?\\d{2}.?<extension>$

This works!!!.

Thanks for the tip
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date and time search using regular expression.