Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Best solution for Regex problem

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This question is based on the example given in Sun Certified Programmer guide by Kathy . Assume I have a String "abc,def,proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java,project,projectile,projection,projection.txt", which basically denotes the list of all available files and folders. I want to retrieve "Only" folders. That is my outcome should be
abc
def
proj1
proj2
project
projectile
projection

I tried to workout on this problem, taking the entire string at a time. But I could not find any solution for that.Hence I tokenized the string dividing them into individual tokens and checked whether there is '.' in each and every token thus making it disqualified to be called as a folder. This logic worked, but I know this is not what I was looking for. How do I list out only folders by taking the entire string at a time? What is the pattern I can use here. Thanks in advance.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can try this...



But keep in mind that a long solution that you understand is better than a short solution that you don't.

Henry
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. The solution is really awesome, though I'm not really comfortable at the pattern .

But keep in mind that a long solution that you understand is better than a short solution that you don't.

This is very much true in my case. Would you mind suggesting any resource to learn more about these regex as Kathy's book on SCJP is more exam oriented and most of the expression you used is not covered in the book.

Once again thanks for the solution.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Kota:
This is very much true in my case. Would you mind suggesting any resource to learn more about these regex as Kathy's book on SCJP is more exam oriented and most of the expression you used is not covered in the book.



Unfortunately, I learned via years of programming with sed, awk, TCL and of course, Perl -- the Perl online docs are actually very good.

I have never read a Java book on Regex, but one of Javaranch's own Sheriffs wrote one...

http://www.amazon.com/exec/obidos/tg/detail/-/1590591070/qid=1061263675/sr=1-7/ref=sr_1_7/103-4032505-6197422?v=glance&s=books#product-details

Henry
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information Henry. Nothing beats the earning from experience .
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic