wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Regular Expression" Watch "Help with Regular Expression" New topic
Author

Help with Regular Expression

Vivek Sam
Greenhorn

Joined: Jul 29, 2004
Posts: 8
Hi ,

I have a file name of the following format "Titan20110801P96584" . Now i want to split the filename using a regular expression and drop the date part in the file name.

I tried the regular expression [\\d] , but that drops all the numbers , but I only want to drop the 20110801 from the file name , and retain the rest.

Can anybody help.

Thanks in advance,
Vivek
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Which part is the date part?

The trick to coming up with the regular expression is to know what you are trying to get. How would you tell someone, who had no idea what a date was, what part you want to keep and what part you want to get rid of?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vivek Sam
Greenhorn

Joined: Jul 29, 2004
Posts: 8
Hi,

The date part is 20110801 .

Now if I had to tell somebody who had no idea what a date was , I would want to keep the first text part of the filename till I encounter the first number , and then skip the numbers which follow the the format YYYY-MM-DD , and then pick up the rest of the characters after that to give me the final name as TitanP96584.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

So you have a bunch of letters, then a bunch of digits, then some more stuff which starts with a letter. And you want to drop the bunch of digits. Is that right?

(I could also ask if you wanted to drop the bunch of digits even if they didn't represent a date, but let's not do that just yet.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

I don't know what a date is, so telling me "YYYY-MM-DD" doesn't mean anything. Further, I don't see any dashes in the file name, so what does that have to do with anything?
Bhavani Sankar Ikkurthi
Greenhorn

Joined: Aug 07, 2008
Posts: 8

Vivek Sam wrote:Hi,

The date part is 20110801 .

Now if I had to tell somebody who had no idea what a date was , I would want to keep the first text part of the filename till I encounter the first number , and then skip the numbers which follow the the format YYYY-MM-DD , and then pick up the rest of the characters after that to give me the final name as TitanP96584.


You could many Regex tools available online. See if this works for you.
\d{8}
I assumed the date format will always be of 8 numbers (YYYYMMDD) and the characters of the file-name [at beginning] (Titan) don't end with a number.
Vivek Sam
Greenhorn

Joined: Jul 29, 2004
Posts: 8
@Paul
Yes , thats right

@Fred
Ok , so generally speaking it would just be a sequence of digits that follow , and the length of which would not exceed 8 digits.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

would it not exceed 8, or would it be exactly 8?

are you guaranteed a letter after the date, before the other numeric part?
Vivek Sam
Greenhorn

Joined: Jul 29, 2004
Posts: 8
@fred
Yes , it would not be greater than 8 , and a letter will follow before the non-numeric character

@Bhavani
Thanks for the input , I will try and understand the same , and then refine it further to use it in my code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Regular Expression
 
Similar Threads
Regular Expression that checks for file names
regex
regex question -- looking for ? that is NOT inside of quotes
Trouble with basename task property in a for task
Validating Cron Expression