The_Kingpin08
asked on
Word wildcard search
Hi everybody,
I want to create a word search engine using the wildcard (*). I've made some research on the net and here for more information and found something about regex.
his seems to be the best way to do it but I'm not sure I understand correctly, so I'd like if someone could help me with my function.
Here it is:
public void search (String searchedWord)
{
// We go throught the vector of words and compare every words.
// The vector "words" is already defined and filled.
for (int i = 0; i < words.size(); i++)
{
String currWord= (String) words.elementAt(i);
// This represent the current word of the vector ?
Pattern p = Pattern.compile(currWord);
// This is the searched word with the wildcard (which is always *) ? I'm not sure how to use this...
Matcher m = p.matcher(searchedWord);
// We compare both words and print if it matches
if (m.matches()) System.out.println(currWor d);
}
}
This is how it should works. The user enters a word with any numbers of wildcard, which will always be represented by a *. We then start the research and print every word that match. Thanks,
Frank
I want to create a word search engine using the wildcard (*). I've made some research on the net and here for more information and found something about regex.
his seems to be the best way to do it but I'm not sure I understand correctly, so I'd like if someone could help me with my function.
Here it is:
public void search (String searchedWord)
{
// We go throught the vector of words and compare every words.
// The vector "words" is already defined and filled.
for (int i = 0; i < words.size(); i++)
{
String currWord= (String) words.elementAt(i);
// This represent the current word of the vector ?
Pattern p = Pattern.compile(currWord);
// This is the searched word with the wildcard (which is always *) ? I'm not sure how to use this...
Matcher m = p.matcher(searchedWord);
// We compare both words and print if it matches
if (m.matches()) System.out.println(currWor
}
}
This is how it should works. The user enters a word with any numbers of wildcard, which will always be represented by a *. We then start the research and print every word that match. Thanks,
Frank
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> ok so if I understand correclty, this is a pain if you want to compare a variable containing a string ?
Not sure what you mean - my examples could just as easily be used on a variable:
String s = "cat";
s.matches("c.t");
Not sure what you mean - my examples could just as easily be used on a variable:
String s = "cat";
s.matches("c.t");
ASKER
ok, I thought you had to put a "." after each letter...
Thanks for the information, I'll go try it out
Frank
Thanks for the information, I'll go try it out
Frank
ASKER
If the matches method works on variables too, could I use it this way:
// ************************** ********** ********** **********
public void search (String searchedWord)
{
// We go throught the vector of words and compare every words.
// The vector "words" is already defined and filled.
for (int i = 0; i < words.size(); i++)
{
String currWord= (String) words.elementAt(i);
// We compare both words and print if it matches
if (currWord.matches(searched Word)) System.out.println(currWor d);
}
}
// ************************** ********** ********** **********
This way I get some errors:
Please enter a word to search: MA**N
Exception in thread "main" java.util.regex.PatternSyn taxExcepti on: Dangling meta character '*' near index 3
MA**N
^
at java.util.regex.Pattern.er ror(Unknow n Source)
at java.util.regex.Pattern.se quence(Unk nown Source)
at java.util.regex.Pattern.ex pr(Unknown Source)
at java.util.regex.Pattern.co mpile(Unkn own Source)
at java.util.regex.Pattern.<i nit>(Unkno wn Source)
at java.util.regex.Pattern.co mpile(Unkn own Source)
at java.util.regex.Pattern.ma tches(Unkn own Source)
at java.lang.String.matches(U nknown Source)
at Cruciverbiste.chercher(Cru civerbiste .java:50)
at Numero1.<init>(Numero1.jav a:39)
at Numero1.main(Numero1.java: 27)
>Exit code: 1
Anyone knows what I'm doing wrong ? Thanks
Frank
// **************************
public void search (String searchedWord)
{
// We go throught the vector of words and compare every words.
// The vector "words" is already defined and filled.
for (int i = 0; i < words.size(); i++)
{
String currWord= (String) words.elementAt(i);
// We compare both words and print if it matches
if (currWord.matches(searched
}
}
// **************************
This way I get some errors:
Please enter a word to search: MA**N
Exception in thread "main" java.util.regex.PatternSyn
MA**N
^
at java.util.regex.Pattern.er
at java.util.regex.Pattern.se
at java.util.regex.Pattern.ex
at java.util.regex.Pattern.co
at java.util.regex.Pattern.<i
at java.util.regex.Pattern.co
at java.util.regex.Pattern.ma
at java.lang.String.matches(U
at Cruciverbiste.chercher(Cru
at Numero1.<init>(Numero1.jav
at Numero1.main(Numero1.java:
>Exit code: 1
Anyone knows what I'm doing wrong ? Thanks
Frank
You didn't read my first comment carefully. '*' means something specific in regexes. You need to change:
>>MA**N
to
MA\\w\\wN
to match word characters only
>>MA**N
to
MA\\w\\wN
to match word characters only
ASKER
CEHJ,
that means that for every searched word I gotta go through every letters of the word, find the wildcards and replace them by the character "\\w" ? Doesn't that seems inefficient ? I decided to use regex because it seems the easier and best way to do it, but wouldn't there be a better way to do it ?
BTW sorry for not understanding your first post, I never worked with regex yet.. Thanks for your time,
Frank
that means that for every searched word I gotta go through every letters of the word, find the wildcards and replace them by the character "\\w" ? Doesn't that seems inefficient ? I decided to use regex because it seems the easier and best way to do it, but wouldn't there be a better way to do it ?
BTW sorry for not understanding your first post, I never worked with regex yet.. Thanks for your time,
Frank
>>that means that for every searched word I gotta go through every letters of the word, find the wildcards and replace them by the character "\\w" ? Doesn't that seems inefficient ?
Not really - it's really pretty simple ;-)
String s = "M**N";
s = s.replaceAll("\\*", "\\\\w");
Not really - it's really pretty simple ;-)
String s = "M**N";
s = s.replaceAll("\\*", "\\\\w");
ASKER
ok I'll shut up, you got me now ;)
Thanks a lot for the help, I appreciate a lot !
Frank
Thanks a lot for the help, I appreciate a lot !
Frank
:-)
>> I want to create a word search engine
Not sure how complex your search engine should be (and how much data it should contain)
But if you need something serious then you might want to look at lucene:
http://lucene.apache.org/java/docs/
Not sure how complex your search engine should be (and how much data it should contain)
But if you need something serious then you might want to look at lucene:
http://lucene.apache.org/java/docs/
ASKER
Any other way to do this ?