aspose file tools*
The moose likes Java in General and the fly likes Finding classes that match a user supplied regexp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding classes that match a user supplied regexp" Watch "Finding classes that match a user supplied regexp" New topic
Author

Finding classes that match a user supplied regexp

Nandini Kalelkar
Greenhorn

Joined: Feb 17, 2013
Posts: 7
Below is my code. I am trying to display only the classes that match the expression user entered. so, for the case when the user enters I want the program to match all the strings in



Please help. What am I doing wrong here?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4180
    
  21

Nandini Kalelkar wrote:. What am I doing wrong here?

Dunno, why don't you tell us how the application behaves - what is the incorrect behavior and how does it differ from what you expect?

Also, you use the object's toString() method to get the name of the class: why? Aren't you trusting a lot that the toString() method hasn't been modified so it provides a different format of output? Wouldn't you be safer using o.getClass().getName()?
Nandini Kalelkar
Greenhorn

Joined: Feb 17, 2013
Posts: 7
Sorry for the inconvenience.

Behavior should be - If the user enter "person*" then a class like 'org.apache.console.thriftobjs.Person' from the inputSet should be able to be matched and added to the resultSet. I have tried different regexps as per my understanding. One of them being



isn't that supposed to work?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

Nandini Kalelkar wrote:Sorry for the inconvenience.

Behavior should be - If the user enter "person*" then a class like 'org.apache.console.thriftobjs.Person' from the inputSet should be able to be matched and added to the resultSet. I have tried different regexps as per my understanding. One of them being



isn't that supposed to work?

That regex says:
the beginning of a line (^), followed by
exactly one character(.), followed by
the literal string "perso", followed by
zero or more 'n' characters (n*), followed by
exactly one character (.)

I don't think that's what you want.

The trick with regular expressions is to first define in ENGLISH what you want to match. Only after you can do that should you try writing the regex. So, what do you want to match? Assume the user inputs "person*". You might want to match

the beginning of a line, followed by
zero or more characters, followed by
the regex the user gave you, followed by
zero or more characters.

of course, that may NOT be what you want - we have no way of knowing unless you tell us.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4180
    
  21

Nandini Kalelkar wrote:Sorry for the inconvenience.

Behavior should be - If the user enter "person*" then a class like 'org.apache.console.thriftobjs.Person'


"person*" means the characters perso followed by n 0 or more times. It would match perso, person, personnnnn, but not Person (capital P) or something.person (because something. comes before perso) nor would person1 (because there is a character not in the regex).

from the inputSet should be able to be matched and added to the resultSet. I have tried different regexps as per my understanding. One of them being



isn't that supposed to work?


That means the start of the string immediately followed by any one character followed by the exact characters p e r s o followed by n 0 or more times, followed by any one character.

You should brush up on regular expressions (see the Pattern class and the tutorial.) Or you should see if you can do what you want another way, because regex is not easy, and you shouldn't expect your users to know it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding classes that match a user supplied regexp