• Post Reply Bookmark Topic Watch Topic
  • New Topic

search mechanism in java  RSS feed

 
Anand das
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I need to write an API for search string function. user eneters either full string or part of the string to look for the results. (eg: like %like% in sql )
How do I implement this in java any exampels would be appreciated..

Thanks
Andy
 
Dipanjan Kailthya
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Regex tutorial:

http://java.sun.com/docs/books/tutorial/essential/regex/

 
Anand das
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
a small example would be fine.
As I need to check this string against the xml data, At the moment i get all the results from xml data into a hashmap.
would it be good to search against this hashmap or search against the XML data file itself.

Thanks
Andy
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For implementing something like "%like%" you don't need regexps - String.indexOf should be sufficient.

Whether caching the XML in memory makes sense depends on how many XML files there are, how large they are, and how often you expect to search them.
 
Anand das
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that gives index of first occuring character
for ex: String color = ""bluegrey";
String find = "eg";
int i = color.indexOf(find); gives 3
where as I want to see exact "eg" is matched , give me some example please.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a leading "%" -as in "%foo"- you can check that there is no alphabetic character before the result position, and for a trailing % -as in "foo%"- you can check that there is no alphabetic character after the result position.

You may want to treat the various cases (foo, %foo, foo%, %foo%) differently, or, yes, you could construct a regexp according to which of those it is. Check out the section on "Boundary matchers" in the java.util.regex.Pattern javadocs.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
andy foo wrote:give me some example please.
You will have to work that out for yourself, I am afraid. Look at our FAQ.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!