Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding classes that match a user supplied regexp

 
Nandini Kalelkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic