This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Regular Expression

 
Vivek Sam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vivek Sam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21128
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic