Link to home
Start Free TrialLog in
Avatar of The_Kingpin08
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(currWord);
      }
}


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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of The_Kingpin08
The_Kingpin08

ASKER

ok so if I understand correclty, this is a pain if you want to compare a variable containing a string ?

Any other way to do this ?
>> 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");  
ok, I thought you had to put a "." after each letter...

Thanks for the information, I'll go try it out

Frank
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(searchedWord)) System.out.println(currWord);
     }
}
// ********************************************************

This way I get some errors:

Please enter a word to search: MA**N
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 3
MA**N
   ^
      at java.util.regex.Pattern.error(Unknown Source)
      at java.util.regex.Pattern.sequence(Unknown Source)
      at java.util.regex.Pattern.expr(Unknown Source)
      at java.util.regex.Pattern.compile(Unknown Source)
      at java.util.regex.Pattern.<init>(Unknown Source)
      at java.util.regex.Pattern.compile(Unknown Source)
      at java.util.regex.Pattern.matches(Unknown Source)
      at java.lang.String.matches(Unknown Source)
      at Cruciverbiste.chercher(Cruciverbiste.java:50)
      at Numero1.<init>(Numero1.java:39)
      at Numero1.main(Numero1.java:27)
>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
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 ?

Not really - it's really pretty simple ;-)

                  String s = "M**N";
                  s = s.replaceAll("\\*", "\\\\w");
ok I'll shut up, you got me now ;)

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/