?
Solved

Word wildcard search

Posted on 2005-04-24
11
Medium Priority
?
450 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:The_Kingpin08
  • 5
  • 5
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13854572
'*' has, you should know, a precise meaning in the regex world. It means 'match none or more of the preceding character'. You mean * in the 'everday' sense. You need to change that to '.' in the regex, e.g.

Look for e.g. c*t

"cat".matches("c.t"); // true
"cot".matches("c.t"); // true
"c,t".matches("c.t"); //  ALSO true, so you may want to do the following:
"cat".matches("c\\wt"); // true
"c,t".matches("c\\wt"); // false
0
 

Author Comment

by:The_Kingpin08
ID: 13854612
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 ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13854640
>> 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");  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:The_Kingpin08
ID: 13854671
ok, I thought you had to put a "." after each letter...

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

Frank
0
 

Author Comment

by:The_Kingpin08
ID: 13854719
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13854791
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
0
 

Author Comment

by:The_Kingpin08
ID: 13854809
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13854835
>>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");
0
 

Author Comment

by:The_Kingpin08
ID: 13854842
ok I'll shut up, you got me now ;)

Thanks a lot for the help, I appreciate a lot !

Frank
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13854849
:-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13856439
>> 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/
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question